Skip to content

Endpoints

Kevin Lee edited this page Feb 12, 2016 · 2 revisions

To build our API endpoints, we're concerned with two parts of the application: Routers and Controllers.

Please review the documention for Laravel's routes, middleware, and RESTful controllers.

###Routing

The routing files can be found at App\Http\Routes. The App\Providers\RouteServiceProvider provider has been configured to include any routes found within the Routes directory.

Our only set of routes defines our api/v1/token endpoint and the RESTful API for retrieving and manipulating users:

Route::group(['prefix' => 'api/v1'], function () {
    //Retrieve or refresh token
    Route::post('token', ['uses' => 'UsersController@token', 'middleware' => 'auth']);

    //Apply VerifyToken middleware to api
    Route::group(['middleware' => 'token'], function () {
        Route::resource('users', 'UsersController', ['except' => ['create', 'edit']]);
    });
});

In particular, the Route::resource method will map the appropriate HTTP requests to a controller's RESTful structure, as we do with the requests to the api/v1/users endpoint to the UsersController here.

####Middleware

HTTP middleware provide a convenient mechanism for filtering HTTP requests entering your application. For example, we include a middleware that verifies the user of your API is authenticated at App\Http\Middleware\Authenticate. If the HTTP request contains a valid access token within the Authorization header, the middleware will forward the request to the API (in our routing gist above, this is currently the UsersController.

Middleware classes can be added to the application to use in routes by adding them and their aliases to the App\Http\Kernel.

###Controllers

Controllers can be found at App\Http\Controllers. These contain methods to execute API endpoints and return an appropriate HTTP response.

Clone this wiki locally