Laravel Route Resources: Efficient URL Handling

Routes are one of the base concepts of your Laravel application, it's here you define where URIs are pointing to in your application. Often the route will point to a controller but it can also direct output content. I will leave it for another post how Model View Controller (MVC) is structured, to understand this post you only have to understand that a route points to a controller which loads a template (view).

A bit simplified it could be illustrated as:

Route => Controller => View

In Laravel routes are powerful because they can be assigned with middlewares, but also easy to manage as they have built in support for RESTful resources. If you follow the principles of RESTful you could save yourself a lot of code.

RESTful

Before continuing I think it would make sense to give a brief introduction to RESTful. RESTful isn't a language or algorithm but a concept or definition of structuring paths. Originating in designing APIs its focus is in delivering such with an easy to understand path structure.

To give an example if you want to list all users, you should think of this as the top level option of the user endpoint, which would therefor be:

GET /users

But if you would like to see just 1 user, this is a secondary option to the endpoint. It could be tempting to use query strings (?key=value), but these should be reserved for filtering and showing a specific user is more a dig down the endpoint than filtering it. Therefor it's done by applying the id as next in the path:

GET /users/1

If you want to update a user, this is the same, but with a PUT method. However HTTP doesn't support this, so different libraries implements differently. Laravel uses a form input called _method to convert a POST to PUT:

PUT /users/1

Same goes for deleting a user:

DELETE /users/1

If you want to create a new user, this is done to the main endpoint, since you wouldn't know any id's at that point:

POST /users

So hopefully you catch the idea what RESTful is all about.

How to use it with Laravel routes

In Laravel you can define a (web) route by it's method:

Route::get();
Route::post();
..

But there's also a neat option that allows you to directly create what's called a route resource, which is an implementation of a RESTful route:

Route::resource('users', '...');

You can chain this by either excluding or including only a set of the resources (they both result in the same):

Route::resource('users', '..')->only(['index', 'show']);
Route::resource('users', '..')->except(['create', 'update', 'destroy', 'store']);

Now you would have access to 2 URLs:

GET /users
GET /users/1

And best of all, they will per default point to methods in the same name in your controller which you easily generate in Laravel with the Artisan command make:controller prefix by --resource:

php artisan make:controller UsersController --resource


In case you wonder. The image was taken on top of the Rock of Gibraltar looking towards Algeciras in Spain.

Emil Moe

Expert in Laravel and Vue backed with MySQL databases. Independent developer who does freelance and love to travel. Feel free to drop me a message.

- Emil

Write your comment…

Be the first one to comment