Open In App

Laravel | Route::resource vs Route::controller

In Laravel, the Route actions can be controlled by any of the following two methods, either by using Route::resource method or by using Route::controller method. But both of them have their differences. Route::resource: The Route::resource method is a RESTful Controller that generates all the basic routes required for an application and can be easily handled using the controller class. It takes two arguments, where first is the base incoming request URI (Uniform Resource Identifier) and second is the class name of the controller which is used to handle the request. With this method, we get route names and path defined by default with 7 different actions which are required for any application. Note: To get the list of routes created, we have to use the command ‘PHP artisan route: list’ in the command line. Example:

Route::resource(‘gfg’, ‘GeeksforGeeksController’);

Route::controller: The Route::controller method is an Implicit Controller which also takes two arguments and are same as Route::resource method i.e. first is the base incoming request URI (Uniform Resource Identifier) and second is the class name of the controller which is used to handle the request. With this method, we have a little flexibility in how we define our route names. There are no route names defined by default as there were with Route::resource method. Note: To get the list of routes created, you have to use the command ‘PHP artisan route: list’ in the command line. Example:

Route::controller(‘gfg’, ‘GeeksforGeeksController’);
class GeeksforGeeksController extends Controller
{
  // Handle request
}
class GeeksforGeeksController extends Controller
{
  public function getGeek()
  {
    // Handle get request
  }
  public function postGeek($id)
  {
    // Handle post request
  }
}

Before specifying either of the routes, you have to create the controller class which you want to use in the route. You can do that using a simple artisan command that is ‘PHP artisan make: controller GeeksforGeeksController’ i.e. ‘PHP artisan make: controller ’. The Route:Controller method had its support till Laravel 5.1 and is no longer supported by the versions after that. So, in the later versions of Laravel, we use Route::resource method.

Article Tags :