Laravel Rest API, the easy introduction Tutorial

This article follows another one talking about a Symfony Rest API, which we made to exemplify the concept of RESTful applications.

The Php programming language

This is the turn of Laravel, in that we are already able to write a PHP API in a simple way, but we still don’t know how easy it is with this other framework. So let’s get started.

The first thing we need to do, is to install Laravel via Composer. If you’re using PhpStorm or a similar IDE, take a look at its tools. You can create the project and manage all the dependencies through means of those.

$ composer create-project --prefer-dist laravel/laravel laravel_rest_api "5.8.*"
$ cd laravel_rest_api

Let’s suppose you have your MySQL installation on your path and you can create a database for our application:

$ mysql -u root -p
mysql> CREATE DATABASE laravel;
mysql> exit;

Good, let’s create our Entity and Model to save and retrieve our data:

$ php artisan make:model Article -mc

You will find a new migration under database/migrations inside our root folder. And it’s time to modify our newly created migration, in order to include all the necessary fields:

public function up()
{
	Schema::create('articles', function (Blueprint $table) {
		$table->bigIncrements('id');
		$table->timestamps();
		$table->string('title', 100);
		$table->string('author', 50);
		$table->string('body', 1000);
		$table->string('url', 200)->nullable($value = true);
	});
}

And we apply our migration to the database:

$ php artisan migrate

Let’s include our fields inside our Article Model class inside our app folder under the root folder:

class Article extends Model
{
    protected $fillable = ['title', 'author', 'body', 'url'];
}

Then, we’re gonna have to put our application logic on our Controller. Go to app/Http/Controller and find the ArticleController.php file:

class ArticleController extends Controller
{
    public function index()
    {
        return Article::all();
    }

    public function show($id)
    {
        return Article::find($id);
    }

    public function store(Request $request)
    {
        return Article::create($request->all());
    }

    public function update(Request $request, $id)
    {
        $article = Article::findOrFail($id);
        $article->update($request->all());

        return $article;
    }

    public function delete(Request $request, $id)
    {
        $article = Article::findOrFail($id);
        $article->delete();

        return 204;
    }
}

The last step is to configure our API routes, add the following code to api.php inside the routes folder:

Route::get('articles', 'ArticleController@index');
Route::get('articles/{id}', 'ArticleController@show');
Route::post('articles', 'ArticleController@store');
Route::put('articles/{id}', 'ArticleController@update');
Route::delete('articles/{id}', 'ArticleController@delete');

Let’s go to our PHP server to run the application. Don’t use the Laravel wrapper, the application would hang after one call. You would have to restart your server between each API call.

$ cd public
$ php -S localhost:8000

Do remember to test the application, use cURL to create some Articles, see them, update them, delete them. Do as follows:

$ curl -H "Content-Type: application/json" -d "{\"title\":\"Article Title\", \"author\":\"Mirko Benedetti\", \"body\":\"Article Body\", \"url\":\"Article Url\"}" http://localhost:8000/api/articles
$ curl -v http://localhost:8000/api/articles/1

$ curl -H "Content-Type: application/json" -d "{\"title\":\"Second Title\", \"author\":\"Mirko Benedetti\", \"body\":\"Second Body\", \"url\":\"Second Url\"}" http://localhost:8000/api/articles
$ curl -v http://localhost:8000/api/articles

$ curl -X PUT -H "Content-Type: application/json" -d "{\"title\":\"Updated Title\", \"author\":\"Francesco Angeli\", \"body\":\"Updated Body\", \"url\":\"Updated Url\"}" http://localhost:8000/api/articles/1
$ curl -v http://localhost:8000/api/articles/1

$ curl -X DELETE http://localhost:8000/api/articles/1
$ curl -v http://localhost:8000/api/articles

This Tutorial is based on Windows with the help of Git for Windows, please go to this GitHub for the full code of the application.

Did you like this post? Please, share it on your preferred social networks or comment here below, thank you!

Leave a Reply

Give me your opinion, I will be grateful.