DEV Community

Cover image for Take a shorter route with RESTful controllers
FranciskaDendooven
FranciskaDendooven

Posted on • Updated on

Take a shorter route with RESTful controllers

Use RESTful controllers to turn your routes into more structured and clean one-line code. Who wouldn't like that? I followed a video-tutorial on making a CRUD in Laravel (inertia) with ReactJs. The video showed a route that uses route::resource(); to direct to the controller and make use of the index, create, store, update and destroy methods. I saw it as a simpler and cleaner way than using 6 to 8 routes like: route::get(''); , route::post(''); , ... for example. Feel tempted to take a shortcut?
Well, I certainly was.

This is the code with a RESTful controller:

Route::resource('posts', PostController::class); 
Enter fullscreen mode Exit fullscreen mode

And it all worked fine! Until I tried implementing a second CRUD on the same page.jsx through a different controller. It gave an error that it could not find the show-method. The pre defined system got confused between the one show-method of the first CRUD and the show-method of the second CRUD, even though the route was directed to two different controllers.

Code that solved the error:

Route::get('/Posts', [PostController::class, 'show']);

Route::get('/Posts/comment', [PostCommentController::class, 'show']);
Route::post('/Posts/comment', [PostCommentController::class, 'store']);

Route::post('/Posts/remove/{id}', [PostController::class, 'destroy']);
Route::get('/Posts/create', [PostController::class, 'create']);
Route::post('/Posts/create', [PostController::class, 'store']);
Route::get('/Posts/update/{id}', [PostController::class, 'edit']);
Route::put('/Posts/update/{id}', [PostController::class, 'update']);
Enter fullscreen mode Exit fullscreen mode

What exactly are RESTful controllers?
REST means REpresentational State Transfer, a software architectural style that describes a machine to machine interface between physically seperate components. REST allows web developers to display content when requested, in a dynamic way.
RESTful dynamic content uses server-side rendering to display a website. Take a look at the next example:

Image description

Put otherwise, it is a infrastructural style and not a toolkit.

Unknowingly I used a RESTful controller. This controller allows you to use a single route for all the methods in the routes.php file. The example below gives the syntax needed to create a RESTful Controller:

Route::resource('app', 'AppController');
Enter fullscreen mode Exit fullscreen mode

How do we build a resource controller quickly?
Use your terminal to insert the artisan command make:controller –resource. Like so:

php artisan make:controller ResourceController resource
Enter fullscreen mode Exit fullscreen mode

This command will generate a controller file at

app/Http/Controllers/ResourceController.php
Enter fullscreen mode Exit fullscreen mode

and also create a Resource class at

app/Http/Resources/Resource.php
Enter fullscreen mode Exit fullscreen mode

Resource controllers make it effortless to build RESTful controllers around resources. Although some documentation state that Resource controllers and RESTful controllers refer to the same thing. The Laravel 9.x uses the name Resource Controllers. In this article I am going to use the term RESTful Controllers, since it was the first clue I found while researching about this controller and route.

RESTful controllers use the standard blueprint for a RESTful resource. This blueprint consists of:

Image description

The actions above all have a default route name, which you can override if you prefer something more suitable according to yourself and the project you are working on. You simply pass a names array with your option:

Route::resource('app', 'AppController', ['names' => ['create' => 'app.build']]);
Enter fullscreen mode Exit fullscreen mode

You can even link many Resource Controllers in one go by passing an array to the resources method:

Route::resources([
'app' => AppController::class,
'anotherApp' => AnotherAppController::class,
]);
Enter fullscreen mode Exit fullscreen mode

Now if you want to add additional routes to your Controller, outside the default set of resource routes, you have to define them before you call the resource method. Like so:

use App\Http\Controller\AppController;

    Route::get('/YourPage/popular', [AppController::class, 'popular']);
    Route::resource('yourpages', AppController::class);
Enter fullscreen mode Exit fullscreen mode

You can also partially declare a Resource Route by specifying a handle action on the route:

Route::resource('app', 'AppController',
['only' => ['index', 'show']]);

Route::resource('app', 'AppController',
['except' => ['create', 'store', 'update', 'destroy']]);
Enter fullscreen mode Exit fullscreen mode

Next to renaming and partially declaring you can lastly also nest Resources. Imagine you need multiple comments attached to a post, picture,... In that case you may use a period notation in your route:

use App\Http\Controllers\AppCommentController;

        Route::resource('app.comments', AppCommentController::class);
Enter fullscreen mode Exit fullscreen mode

What now?
RESTful controllers are controllers that contain the CRUD functions for the model or nested models. I eventually changed the RESTful controller method into using different routes for all the shows, edits, updates, destroys. This was before I did this research and found you can link many RESTful controllers at once. Patience is key.
At the end of the day, a RESTful controller is just a controller. Or a Resource Controller according to Laravel 9.x. But when to use a RESTful Controller? For me personally, I am going to use it to avoid a lot of different routes to the same Controller. Espescially now that I am up to date with the term RESTful Controller. According to research some also find the method confusing. I guess, it's up to you to decide which method for routing suits you best.

Documentation:
-Phil, https://stackoverflow.com/questions/22933301/laravel-4-difference-between-restful-controllers-and-resource-controllers-in-la, 30/10/2014, found on 06/10/2022

-Laravel documentation, https://laravel.com/docs/9.x/controllers#resource-controllers, found on 06/10/2022

-TutorialsPoint, https://www.tutorialspoint.com/laravel/laravel_controllers.htm#, found on 10/10/2022

Top comments (1)

Collapse
 
soneoso profile image
Eli Dekeersgieter

Nice job!