Skip to content

Instantly share code, notes, and snippets.

@prodeveloper
Last active October 25, 2016 07:01
Show Gist options
  • Select an option

  • Save prodeveloper/7819b6e745f08ace9828600bf3ee2ca7 to your computer and use it in GitHub Desktop.

Select an option

Save prodeveloper/7819b6e745f08ace9828600bf3ee2ca7 to your computer and use it in GitHub Desktop.
Basics of working with APIs

##What is an API

In previous classes, we have been using forms and tables to input and display data respectively.

This works well for pure web applications. When we want to open up our application for integration, then we need an API (Application Programming Interface).

An API allows programmatic access to your application. This means that integrations such as:

  • Mobile application
  • Cron jobs
  • Third party applications

Can now be built to extend the application. In this lesson, we will be looking at how to open up our Student model.

##Step 1 Get a client

Since we are building an interface, we need something to integrate with that interface for testing purposes.

For this, we are going to use Postman

Download the chrome client.

Postman allows us to carry out all HTTP verbs.

##Step 2 Write the controller

We create the StudentApiController by running the following command

php artisan make:controller StudentApiController --resource

This will give us a listing of all actions available to the students resource.

Since we don't have any forms we can get rid of the form methods.

  • create
  • edit

##Step 3 Controller methods

The biggest difference between an API controller and a normal controller is that we return the results as JSON. In a normal controller, we usually return a view.

Our StudentApiController would thus look like this.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App;

class StudentApiController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return ['students'=>App\Student::paginate(10)];
    }

   

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
         $student = new App\Student();
        $student->name= $request->get('name');
        $student->email= $request->get('email');
        $student->course= $request->get('course');
        $student->save();
        return ['status'=>'success','message'=>'New student saved'];
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $student= App\Student::findOrFail($id);
        return $student;
    }

   

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        
        $student=App\Student::findOrFail($id);
        $student->name= $request->get('name');
        $student->email= $request->get('email');
        $student->course= $request->get('course');
        $student->save();
        return ['status'=>'success','message'=>'Student updated'];
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

##Register the controller

With our controller built out, we now need to register it in the routes/api.php file.

As you may remember, from Laravel 5.3 the routes.php file was split up into 3.

  • api.php
  • console.php
  • web.php

Register the controller to the routes/api.php as

Route::resource('students','StudentApiController');

We can now access all the methods by appending api to the url.

Eg

GET request to api/students calls the index method on StudentApiController POST request to api/students calls the store method on StudentApiController

and so on.

See https://laravel.com/docs/5.3/controllers#resource-controllers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment