Using Resource Routes and Controller in Laravel

Using resource is one of the good way of making restful api end points in laravel. We can easily make a set of routes and associated functionality on controller with naming conventions as well.

We start with resource controller. Let’s take an example of an article.

Now, we can create ArticlesController using following artisan command.

Now, just look at the controller inside app/Http/Controllers/ directory you will see following methods in ArticlesController,

When we use resource option while creating controller then all restful methods are created. We can add all necessary codes to make the above methods working as per our requirements.

Custom helper function in laravel

Now, we can create routes for each of these controller methods by adding following line to web.php

Above code will register all restful routes to the ArticlesController. We can list out the articles routes using artisan command like this,

Registered routes are:

In this way, we can create resource controller with restful methods so that we can register routes to those methods accordingly.



Computer Enginner, A Laravel Developer,

Leave a Reply

Your email address will not be published. Required fields are marked *