Simple tools for building robusts APIs

Laravel Montréal #13 - January 28th, 2016

Before we start ...

Thanks for comming !

Jobs

Get involved

Starting simple

Handling errors

Do not use the browser...

Setting up the Controller


php artisan controller:make ContactsController

	

$api->version('v1', function ($api) {
    $api->resource('users', 'App\Http\Controllers\Api\V1\UserController');
});

	

class UsersController extends BaseController {

	public function index() {}          // GET    /
	public function create() {}         // GET    /create
	public function store() {}          // POST   /
	public function show($id) {}        // GET    /1
	public function edit($id) {}        // GET    /1/edit
	public function update($id) {}      // PUT    /1
	public function destroy($id) {}     // DELETE /1

}

	

php artisan api:routes

	

Authentication

Different drivers are available

Let's start with Basic authentication

	

	$api->version('v1', ['middleware' => 'api.auth'], function ($api) {
	    $api->resource('users', 'App\Http\Controllers\Api\V1\UserController');
	});

	

Different drivers are available

Now what about JWT ?

	

	$api->version('v1', ['middleware' => 'api.auth'], function ($api) {
	    $api->resource('users', 'App\Http\Controllers\Api\V1\UserController');
	});

	$api->version('v2', ['middleware' => 'api.auth', 'providers' => ['basic', 'jwt']], function ($api) {
	    $api->resource('users', 'App\Http\Controllers\Api\V1\UserController');
	});

	

Content Negotiation

Use the right codes for the right situation


There's three different trees: x, prs, and vnd. The standards tree you use will 
depend upon the project you're developing.

The unregistered tree (x) is primarily meant for local or private environments.
The personal tree (prs) is primarily meant for projects that are not distributed commerically.
The vendor tree (vnd) is primarily meant for projects that are publically available and distributed.
Subtypes using the personal or vendor trees are technically meant to register with the IANA.
You can configure this in your .env file.

API_STANDARDS_TREE=vnd

	

Accept: application/vnd.LaravelMontrealv1+json

	

Advanced structures

Rate Limiting

Further Reading

That’s All Folks!