11
votes

Someone can please explain what is the difference between RESTful Controllers and Resource Controllers in Laravel ? I also have some Questions-

when should I use RESTful Controllers and when Resource Controllers?

  • Is there any naming convention Of Controller action for RESTful Controllers and Resource Controllers ?

  • If I use RESTful Controllers how could I define route for our controller ?

  • For building API which Controller Method is the best ?

5

5 Answers

17
votes

Laravel Resource Controllers are defined as Route::controller('users', 'UserController'); while Restful Controllers are defined as Route::resource('photo', 'PhotoController');.

A restful controller follows the standard blueprint for a restful resource which mainly consists of:

GET         /resource                    index         resource.index
GET         /resource/create             create        resource.create
POST        /resource                    store         resource.store
GET         /resource/{resource}         show          resource.show
GET         /resource/{resource}/edit    edit          resource.edit
PUT/PATCH   /resource/{resource}         update        resource.update
DELETE      /resource/{resource}         destroy       resource.destroy

While the resource controller isn't opinionated like the restful controller. It allows you to create methods directly from you controller and it all gets automatically mapped to your routes:

public function getIndex()
{
    // Route::get('/', 'Controller@getIndex');
}

public function postProfile()
{
    // Route::post('/profile', 'Controller@postProfile');
}

Will automatically have the routes like Route::post('/profile', 'Controller@postProfile'); without explicitly defining it on the routes, much more of a helper if you will to avoid very long route files.

Doing php artisan routes will show you all your routes. You can test stuff out and use that command to see what routes gets automatically generated.

5
votes

They are different concepts. In laravel, a resource controller defines all the default routes for a given named resource to follow REST principles.

So when you define a resource in your routes.php like:

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

The only thing Laravel does is define for you this routes:

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

And expects that you define those methods on your controller. You can also use only/except clauses to remove unneeded routes:

Route::resource('user', 'UserController', ['except' => ['destroy']]);

More on this on Laravel's documentation.

2
votes

It's just a distinction about the routing declaration. Instead of using one of those, manually define all of your routes.

Route::get(...);
Route::post(...);
Route::put(...);
Route::delete(...);
Route::patch(...);

It makes your routes file authoritative, easy to understand, and less buggy.

2
votes

The documentation currently shows RESTful and Resource controllers to refer to the same thing.

Route::resource('resource', 'ResourceController');

It defines the routes for the following http request verbs mapped to the URI, controller action, and route. This allows you to use the predefined route names to connect to predefined controller actions and will map resource_id to {resource} as shown.

Verb      URI                                  Action  Route Name
GET       /resource/index.blade.php             index   resource
GET       /resource/create.blade.php            create  resource.create
POST      /resource                             store   resource.store
GET       /resource/{resource}/show.blade.php   show    resource.show
GET       /resource/{resource}/edit.blade.php   edit    resource.edit
PUT/PATCH                                       update  resource.update
DELETE                                          destroy resource.destroy

The term Implicit Controller seems to be the term to specify the use of

Route::controller('resource', 'ResourceController');

which will magically connect all routes to to ResourceController so that the http request verb (get/post) is prefixed in the function name used in the controller. This maps any URI to the controller action (function) with (get/put) in front but does not map resource_id to {resource} or route names.

class UserController extends BaseController {

    public function getIndex()
    {
        //
    }

    public function postProfile()
    {
        //
    }

    public function anyLogin()
    {
        //
    }
}

maps to

Verb      URI                  Action           Route Name
GET       /index               getIndex         
POST      /profile             postProfile      
GET       /login               anyLogin         
POST      /login               anyLogin         
DELETE    /login               anyLogin         

It's up to you to decide which method to use if any for routing. There is some debate as to what is useful and if routing is even worth the confusion it can cause.

0
votes

RESTful Resource Controllers

Resource controllers make it easier to build RESTful controllers around resources. For example, you may wish to create a controller that manages "photos" stored by your application. Using the controller:make command via the Artisan CLI and the Route::resource method, we can quickly create such a controller.

To create the controller via the command line, execute the following command:

php artisan controller:make PhotoController

Now we can register a resourceful route to the controller:

Route::resource('photo', 'PhotoController');

This single route declaration creates multiple routes to handle a variety of RESTful actions on the photo resource. Likewise, the generated controller will already have stubbed methods for each of these actions with notes informing you which URIs and verbs they handle.

Actions Handled By Resource Controller

http://laravel.com/docs/5.0/controllers#restful-resource-controllers