ChatGPT解决这个技术问题 Extra ChatGPT

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?

Just a note for Laravel 5.2 users, meanwhile implicit controllers are deprecated.

K
Karol Sobański

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.


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)?
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 - resource.edit is to show an edit View, basically, the form for editing an existing resource.
@fungku That's interesting.. so are you saying that resource.edit would actually return HTML instead of JSON?
@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.
A
Ahmad Sharif

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.