Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel-4: Difference between RESTful Controllers and Resource Controllers in Laravel

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 ?

like image 325
Asfaq Tamim Avatar asked Apr 08 '14 09:04

Asfaq Tamim


People also ask

How do I create a resource controller in Laravel?

Type php artisan make:controller sharkController —resource from the command line in the root directory of your Laravel project. Our resource controller will be created with all of the methods we require. What is use of resources in Laravel?

What is the Laravel Service container used for?

The Laravel service container is used to resolve all Laravel controllers. As a result, you are able to type-hint any dependencies your controller may need in its constructor. The declared dependencies will automatically be resolved and injected into the controller instance:

Why build REST API with Laravel?

With the rise of Mobile Development and JavaScript frameworks, using a RESTful API is the best option to build a single interface between your data and your client. Open in app Home Notifications Lists Stories Write Published in TechvBlogs Smit Pipaliya Follow Jan 27 9 min read Save Build REST API with Laravel

How to control the route actions in Laravel?

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.


2 Answers

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.

like image 120
majidarif Avatar answered Sep 19 '22 01:09

majidarif


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.

like image 38
Phil Avatar answered Sep 21 '22 01:09

Phil