We will create simple application that solve all your queries regarding Restful APIs in Laravel 5.7.19 and mySql
I assume that you have basic knowledge of PHP and laravel. We will create a sample Restful APIs with Authors and their blogs relations, so you will understand easily .
PHP 7.1.3 Laravel 5.7.* Mysql DB
I am assuming that you already setup your server and php/mysql, so we can jump directly learning stuff which is the main purpose of this tutorial.
First of navigate to your working directory and install Laravel by composer
Syntax
composer create-project laravel/laravel {projectName} "5.7.*" --prefer-dist
Or
composer create-project --prefer-dist laravel/laravel:5.7.* {projectName}
Example:
composer create-project --prefer-dist laravel/laravel laraapi
Let's start playing with code now !!
First of all we will create Models and Controllers by using artisan CLI
php artisan make:model Model/Author –a
-a option denotes --all | It will Generate a migration, factory, and resource controller for the model, all at once ;)
Similarly, now we will create Blog Model by using -a option
php artisan make:model Model/Blog –a
So, Model, Factory, Migration and Controller created at once. Thanks to artisan :)
As we are creating APIs here, so i recommend to move resource Controllers created for API purpose under API directory, so new path of our Controller will be:
As we moved our Controllers, so we need to regenerate namespaces for autoload, so need to run below command,
composer dump-autoload
it will associate our controller paths correctly in below files:
vendor\composer\autoload_classmap.php
&
vendor\composer\autoload_static.php
One more thing, as we moved our controllers inside some other directory i.e API, so we also need to use Controller namespace now inside our Controllers, so please update namespace and add below lines in AuthorController.php and BlogController.php respectively:
namespace App\Http\Controllers\API
use App\Http\Controllers\Controller;
Thats it, now your copy is up to date!
Navigate to api.php under routes
'routes\api.php'
First, we will add new route as given below:
Route::Resource('/authors','API\AuthorController');
and run below command in terminal:
php artisan route:list
It will show all routes as :
Method | URI | Name | Action | Middleware |
---|---|---|---|---|
GET | HEAD | / | Closure | |
GET | HEAD | api/authors | authors.index | App\Http\Controllers\API\AuthorController@index |
POST | api/authors | authors.store | App\Http\Controllers\API\AuthorController@store | api |
GET | HEAD | api/authors/create | authors.create | App\Http\Controllers\API\AuthorController@create |
GET | HEAD | api/authors/{author} | authors.show | App\Http\Controllers\API\AuthorController@show |
PUT | PATCH | api/authors/{author} | authors.update | App\Http\Controllers\API\AuthorController@update |
DELETE | api/authors/{author} | authors.destroy | App\Http\Controllers\API\AuthorController@destroy | api |
GET | HEAD | api/authors/{author}/edit | authors.edit | App\Http\Controllers\API\AuthorController@edit |
Now, we will add api with Resource like:
Route::apiResource('/authors','API\AuthorController');
now again check routes list by using artisan, you will see difference
php artisan route:list
It will remove create and edit from route list, see below:
Method | URI | Name | Action | Middleware |
---|---|---|---|---|
GET | HEAD | / | Closure | |
GET | HEAD | api/authors | authors.index | App\Http\Controllers\API\AuthorController@index |
POST | api/authors | authors.store | App\Http\Controllers\API\AuthorController@store | api |
GET | HEAD | api/authors/{author} | authors.show | App\Http\Controllers\API\AuthorController@show |
PUT | PATCH | api/authors/{author} | authors.update | App\Http\Controllers\API\AuthorController@update |
DELETE | api/authors/{author} | authors.destroy | App\Http\Controllers\API\AuthorController@destroy | api |
This is how apiResource, is helping us by removing unwanted routes, as when using APIs we don't require any page for creating and for editing purpose.
Similarly, let's create Blog Route also quickly:
Here, as we have to access all blogs associated with particular author like:
// author/{id}/blogs
so, let's create prefix for authors/
Route::prefix('/authors')->group(function () {
Route::apiResource('{author}/blogs','API\BlogController');
});
Now, check route:list once again, to see if everything is working fine.
Method | URI | Name | Action | Middleware |
---|---|---|---|---|
GET | HEAD | / | Closure | |
GET | HEAD | api/authors | authors.index | App\Http\Controllers\API\AuthorController@index |
POST | api/authors | authors.store | App\Http\Controllers\API\AuthorController@store | api |
GET | HEAD | api/authors/{author} | authors.show | App\Http\Controllers\API\AuthorController@show |
PUT | PATCH | api/authors/{author} | authors.update | App\Http\Controllers\API\AuthorController@update |
DELETE | api/authors/{author} | authors.destroy | App\Http\Controllers\API\AuthorController@destroy | api |
GET | HEAD | api/authors/{author}/blogs | blogs.index | App\Http\Controllers\API\BlogController@index |
POST | api/authors/{author}/blogs | blogs.store | App\Http\Controllers\API\BlogController@store | api |
GET | HEAD | api/authors/{author}/blogs/{blog} | blogs.show | App\Http\Controllers\API\BlogController@show |
PUT | PATCH | api/authors/{author}/blogs/{blog} | blogs.update | App\Http\Controllers\API\BlogController@update |
DELETE | api/authors/{author}/blogs/{blog} | blogs.destroy | App\Http\Controllers\API\BlogController@destroy | api |