Creating controllers and routes
We are almost there, but we have a few steps left so that we can finish our API. Now, is time to create the API controller and API routes.
With the newest version (5.6) from Laravel we have a new command available to do this task. This is the --api
flag. Let's see how it works in practice.
Creating and updating the controller function
Open your Terminal window and type the following command:
php artisan make:controller API/BuilderController --api
Note that the --api
flag creates four methods for us inside the BuilderController
class:
index()
= GETstore()
= POSTshow($id)
= GETupdate(Request $request, $id)
= PUTdestroy($id)
= POST
Open
project/app/Http/Controllers/API/BuilderController.php
and add theApp\Builder
code right after the Controller import.Now, let's add the content for each method. Open
project/app/Http/Controllers/API/BuilderController.php
and replace the content with the following code:
<?php namespace App\Http\Controllers\API; use Illuminate\Http...