Sonique
Sonique

Reputation: 7080

Laravel - Route::resource vs Route::controller

I read the docs on the Laravel website, Stack Overflow, and Google but still don't understand the difference between Route::resource and Route::controller.

One of the answers said Route::resource was for crud. However, with Route::controller we can accomplish the same thing as with Route::resource and we can specify only the needed actions.

They appear to be like siblings:

Route::controller('post','PostController');
Route::resource('post','PostController');

How we can choose what to use? What is good practice?

Upvotes: 184

Views: 281620

Answers (4)

Rohan Khatri
Rohan Khatri

Reputation: 61

In Short,

Route::resource('posts', 'PostController');
  • It will allow RESTful routes and by default access methods from the controller which can be customisable.
  • RESTful Resource Controller

Whereas,

Route::controller('posts', 'PostController');
  • All methods of controller will directly accessible from route.

  • Suppose, PostController has getUsers() method,

  • Then route will be, GET posts/users

  • Implicit Controller

To list all available routes, Use php artisan route:list command.

Upvotes: 2

MostafaHashem
MostafaHashem

Reputation: 349

i'm using Laravel 8 in my project

and in my route file web.php i add this route

Route::controller(SitesController::class)->group(function() {  
            Route::get('index', 'index')->name('index');
}
  1. in the Route::controller group we pass controller name we will use
  2. inside the group we define the route we'll use as below syntax

Route::method-used('prefix in the URL', 'function used in the specified controller ')->name(); if the name not used in your code just delete it

Upvotes: 0

ryanwinchester
ryanwinchester

Reputation: 12127

RESTful Resource controller

A RESTful resource controller sets up some default routes for you and even names them.

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

Gives you these named routes:

Verb          Path                        Action  Route Name
GET           /users                      index   users.index
GET           /users/create               create  users.create
POST          /users                      store   users.store
GET           /users/{user}               show    users.show
GET           /users/{user}/edit          edit    users.edit
PUT|PATCH     /users/{user}               update  users.update
DELETE        /users/{user}               destroy users.destroy

And you would set up your controller something like this (actions = methods)

class UsersController extends BaseController {

    public function index() {}

    public function show($id) {}

    public function store() {}

}

You can also choose what actions are included or excluded like this:

Route::resource('users', 'UsersController', [
    'only' => ['index', 'show']
]);

Route::resource('monkeys', 'MonkeysController', [
    'except' => ['edit', 'create']
]);

API Resource controller

Laravel 5.5 added another method for dealing with routes for resource controllers. API Resource Controller acts exactly like shown above, but does not register create and edit routes. It is meant to be used for ease of mapping routes used in RESTful APIs - where you typically do not have any kind of data located in create nor edit methods.

Route::apiResource('users', 'UsersController');

RESTful Resource Controller documentation


Implicit controller

An Implicit controller is more flexible. You get routed to your controller methods based on the HTTP request type and name. However, you don't have route names defined for you and it will catch all subfolders for the same route.

Route::controller('users', 'UserController');

Would lead you to set up the controller with a sort of RESTful naming scheme:

class UserController extends BaseController {

    public function getIndex()
    {
        // GET request to index
    }

    public function getShow($id)
    {
        // get request to 'users/show/{id}'
    }

    public function postStore()
    {
        // POST request to 'users/store'
    }

}

Implicit Controller documentation


It is good practice to use what you need, as per your preference. I personally don't like the Implicit controllers, because they can be messy, don't provide names and can be confusing when using php artisan routes. I typically use RESTful Resource controllers in combination with explicit routes.

Upvotes: 390

Ahmad Sharif
Ahmad Sharif

Reputation: 4435

For route controller method we have to define only one route. In get or post method we have to define the route separately.

And the resources method is used to creates multiple routes to handle a variety of Restful actions.

Here the Laravel documentation about this.

Upvotes: 4

Related Questions