Create a Controller

After running the artisan command to create the module, it has only a single route that is implemented as a closure. A better way is to use a controller to manage all the HTTP methods that we might see in an API.

To create a controller, go to the top level of the API and make the artisan command

# log in to Docker
docker exec -it elentra-developer bash
# navigate to the API root
cd /var/www/vhosts/elentra-1x-me/www-root/core/library/vendor/elentrapackages/elentra-1x-api
# create a widget controller
php artisan make:module:controller widgets WidgetController --resource

The argument --resource tells artisan to create all the route endpoints for the standard set of Http types (GET, POST, PUT, DELETE). Without the argument artisan will create an empty controller for you to fill in your own details.

Now let's change the route to use the controller. Edit the file Widgets/Routes/api.php and replace the closure from previous test with 'WidgetController@index'.

The route will then be simply

Route::get('/widgets', 'WidgetController@index');

Checking the routes with artisan: php artisan route:list --path=widgets you will see the controller now referenced.

| Domain | Method | URI | Name | Action | Middleware |
| | GET|HEAD | widgets | | Entrada\Modules\Widget\Http\Controllers\WidgetController@index | api,api_auth |

At this point you can edit the controller Widgets/Http/Controllers/WidgetController.php and add an echo to the index() method.

public function index()
echo "Hello, Widgets";

Try the route in Postman to verify that the output works.

Next, lets support the standard Http methods

  • GET - for fetching an entry

  • POST - for sending data for a new entry

  • PUT - make an update to an existing entry

  • DELETE - delete an existing entry

Change the route in Widgets/Routes/api.php so that instead of Route::get it says Route::apiResource and remove @index from the controller specification. So the line will look like:

Route::apiResource('/widgets', 'WidgetController');

Now check the routes again with artisan (php artisan route:list --path=widgets)

| Domain | Method | URI | Name | Action | Middleware |
| | GET|HEAD | widgets | widgets.index | Entrada\Modules\Widgets\Http\Controllers\WidgetController@index | api,api_auth |
| | POST | widgets | | Entrada\Modules\Widgets\Http\Controllers\WidgetController@store | api,api_auth |
| | GET|HEAD | widgets/{widget} | | Entrada\Modules\Widgets\Http\Controllers\WidgetController@show | api,api_auth |
| | PUT|PATCH | widgets/{widget} | widgets.update | Entrada\Modules\Widgets\Http\Controllers\WidgetController@update | api,api_auth |
| | DELETE | widgets/{widget} | widgets.destroy | Entrada\Modules\Widgets\Http\Controllers\WidgetController@destroy | api,api_auth |

This has now created a set of routes related to the widgets endpoint. In the Action column you can see the methods that these routes will map to in the controller.

If you perform a GET on /widgets with no trailing identifier, it will go to the index() method in the controller. This is for fetching a list of widgets. If you perform a GET on /widgets/something, then it will use the show() method in the controller to try to find the widget identified by 'something'.

Similarly the POST, PUT and DELETE methods are directed to their unique methods of the controller. The apiResource way of specifying routes is a nice shorthand when you want to create a standard set of routes for some resource.

Some additional cleanup can be done in the controller. Open the file Widgets/Http/Controllers/WidgetController and remove the methods called create() and edit(). These functions are created automatically when you create the controller with Artisan, but they are intended for a Laravel application that includes a front end, where the create() method would display a form to create a new widget, and the edit() function would display a form to edit a widget. Since we are using Laravel only for API functions, these will not be needed in the controller. You can also see that none of the routes use these methods as well.