175

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?

Sonique
  • 6,670
  • 6
  • 41
  • 60
  • 12
    Just a note for Laravel 5.2 users, meanwhile implicit controllers are deprecated. – Roy Jan 21 '16 at 16:18

4 Answers4

373

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.

Karol Sobański
  • 435
  • 5
  • 15
ryanwinchester
  • 11,737
  • 5
  • 27
  • 45
  • 1
    If we use several of Resource routes (maybe index, show) why not use static routes Route::get(...)? I think it's not better not worst than use array('only' => array('index', 'show'). And what method used for RESTFull controller when we request something like 'user/123', getIndex() works for 'user/' but with user/123 I get error NotFoundHttpException (tried different names getView and others, works only when declare as Controller@getView)? – Sonique May 07 '14 at 06:59
  • Can someone clarify what 'resource.edit' is intended for? It is a GET method, so I'm presuming it is supposed to full full information on a resource, rather than just limited information via 'resource.show'? – Anthony Aug 21 '14 at 18:42
  • 2
    @Anthony - `resource.edit` is to show an edit View, basically, the form for editing an existing resource. – ryanwinchester Aug 21 '14 at 22:02
  • @fungku That's interesting.. so are you saying that resource.edit would actually return HTML instead of JSON? – Anthony Aug 22 '14 at 01:02
  • 3
    @Anthony Generally, (and as far as I know) yes. `resource.edit` and `resource.create` are typically for a UI... rendering a view with HTML forms. Those forms would PUT/POST to `resource.update` and `resource.store` respectively. If you are not doing that, then you can just ignore them and get rid of the edit() and create() methods in your controller. – ryanwinchester Aug 22 '14 at 01:27
  • Worst limitation of Implicit controllers is you're limited to a certain route structure. Like `/admin/accounts/{id}/users/{id}` wouldn't work. You'd have to do `/admin/users/{account_id}/{user_id}` or something. Other than that, I kind of like implicit controllers, it's usually pretty easy to find what route is being hit without looking through the routes file. – Captain Hypertext Feb 01 '18 at 14:24
  • Does `Route::controller` syntax even exists in Laravel 5.6? – Amit Shah Sep 20 '18 at 06:24
  • No, After Laravel 5.3, `Route::controller` deprecated – Muhammad Usama Oct 18 '18 at 13:39
  • How to change the `create` route method in Route::resource to 'post' – Jenuel Ganawed Nov 19 '21 at 06:57
3

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.

Ahmad Sharif
  • 4,141
  • 5
  • 37
  • 49
0

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

MostafaHashem
  • 127
  • 2
  • 3
0

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.