Javatpoint Logo
Javatpoint Logo

Laravel Resource Controllers

Laravel resource controllers provide the CRUD routes to the controller in a single line of code. A resource controller is used to create a controller that handles all the http requests stored by your application.

The resource() is a static function like get() method that gives access to multiple routes that we can use in a controller.

Syntax of resource() method:

Route::resource('posts','PostController');

In the above syntax, 'posts' contains all the routes, and 'PostController' is the name of the controller. In this case, we do not need to specify the method name such as @index as we did in get() method because create(), store(), destroy() methods are already available in the PostController class.

Let's understand through an example:

Step 1: Create the controller by using the command given below:

The above command will create the Controller at the app/Http/Controllers/PostController.php directory. The PostController class contains the methods for each resource operations.

The structure of PostController.php file is given below:

Step 2: Now, we need to register the resourceful route to the Controller, and which can be done as follows:

Open the Git Bash Window, and enter the command php artisan route:list. This command produces the following output:

Laravel Resource Controllers

The post parameter in the resource() method produces the names or resources shown in the above output, and its corresponding methods. In the above output, the posts.destroy is sending a parameter to the Delete method, which is very special in Laravel.

Let's understand the concept of resources through an example.

Accessing the show() method of PostController class

Suppose we want to call the show() method of PostController.php file. To do so, add the code in show() method. I added the following code in show() method:

As we know that URI of the posts.show is posts/{posts}, which means that we need to enter the parameter as well to access the show() method of the PostController class.

Suppose I entered the URL as 'localhost/laravelproject/public/posts/58', then the output would be:

Laravel Resource Controllers

Accessing the create() method of PostController class

Step 1: First, we need to add the code in create() method. I added the following code:

As we know that the URI of the posts.create is posts/create, so the URL to access the create() method would be 'localhost/laravelproject/public/posts/create'.

Step 2: Enter the URL 'localhost/laravelproject/public/posts/create' to the browser, then the output would be:

Laravel Resource Controllers

Registering routes for multiple controllers

We can register the routes for multiple controllers by passing an array to the resources() method. Suppose I want to register the routes for two controllers, such as PostController and StudentController. Following are the steps to achieve this:

Step 1: First, you need to create the PostController and StudentController by using the following commands:

Step 2: Add the code given below in web.php file to register routes:

Step 3: Enter the command php artisan route:list on Git Bash Window.

Laravel Resource Controllers

The above screen shows that routes of both the PostController and StudentController are registered.

Partial Resource Routes

When we do not want to register the routes for all the methods, then we can do so by specifying only those functions that the controller can handle.

Steps to create the Partial Resource Routes:

Step 1: First, we create the StudentController by using the below command:


Laravel Resource Controllers

Step 2: Now, we add the following command in web.php file to create the Partial resource routes.

Step 3: To verify whether the above code has registered the routes for the specified methods or not, type the command 'php artisan route:list' on Git Bash Window.

Laravel Resource Controllers

The above screen shows that the routes for create() and show() methods have been generated.

Naming Resource Routes

All the methods of the controller have a default route name, but Laravel allows you to override the route names by passing name array. Name array contains the name of the routes that you want to specify of your choice.

Let's understand the 'Naming Resource Routes' through an example.

  • We can add the below code in web.php file to name the resource routes.
  • Now, enter the command php artisan route:list on Git Bash Window.
Laravel Resource Controllers

The above screen shows that the route name of the create() method has been renamed as student.build, and its default name was student.create.

Naming Resource Route Parameters

Route::resource() method generates the route parameters for all the resource routes automatically, but we can override the route parameters by using the parameters array. The parameters array is an associative array of the resource name and route parameters.

  • We can override the route parameters by adding the following code in web.php file:

The above code assigns the route parameter, i.e., admin_student to the student resource.

  • To check the list of all the route parameters, we use the command given below:
    php artisan route:list.
Laravel Resource Controllers

The above screen shows that the admin_student parameter is added in a route.






Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA