6

what is the difference between static Route methods "resource" and "controller"

Route::controller()

and

Route::resource()

thanks,

tereško
  • 58,060
  • 25
  • 98
  • 150
mwafi
  • 3,946
  • 8
  • 56
  • 83
  • possible duplicate of [Laravel 4 - Route::resource vs Route::controller. Which to use?](http://stackoverflow.com/questions/19102534/laravel-4-routeresource-vs-routecontroller-which-to-use) – Vucko Jun 12 '14 at 07:55
  • I think there are some difference here, when request /url/create , Route::resource request create() method, but Route::controller request getCreate() method – mwafi Jun 12 '14 at 07:58

4 Answers4

6

I got something:

Route::resource()
  • force you to use default methods (index, create, store, show, edit, update, destroy) with no way to add new methods in controller class (no way to call the new method)

but

Route::controller()
  • let you to define unlimited methods inside controller class
  • need to define used HTTP verb before function name like (postCreate, anyCreate)
mwafi
  • 3,946
  • 8
  • 56
  • 83
1

You can read about this in the official documentation:

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

 Route::controller()

It will declare all routes you define as functions starting for html verbs, example from the documentation:

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

  class UserController extends BaseController {

  public function getIndex()
  {
    //
  }

  public function postProfile()
  {
    //
  }

  public function anyLogin()
  {
    //
  }

}

In other hand:

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

Route::resource()

Is basically used when you use the create controller command of artisan:

php artisan controller:make PhotoController

It will generate all the routes generated by the artisan command, basically crud routes.

Hope it helps you.

ruudy
  • 491
  • 2
  • 8
  • 21
1

Here's the routing that occurs when you do both:

Route::controller('test', 'TestController');
Route::resource('othertest', 'OtherTestController');

Here's a picture of what I'm about to write out in text for you, if it's any easier: "php artisan routes" result of the above routes

The following is an all-in-one. For example, if you GET to laravel_dir/test/page, it will look for method getPage() in TestController. If you POST to laravel_dir/test/page, it will look for postPage()

URI: GET|HEAD|POST|PUT|PATCH|DELETE test/{_missing}

Route Name: None

Action: TestController@missingMethod

Below is what results from the resource route... You'll see that it is very useful for CRUD in one line for your routes.php file.

URI: GET|HEAD othertest

Route Name: othertest.index

Action: OtherTestController@index


URI: GET|HEAD othertest/create

Route Name: othertest.create

Action: OtherTestController@create


URI: POST othertest

Route Name: othertest.store

Action: OtherTestController@store


URI: GET|HEAD othertest/{othertest}

Route Name: othertest.show

Action: OtherTestController@show


URI: GET|HEAD othertest/{othertest}/edit

Route Name: othertest.edit

Action: OtherTestController@edit


URI: PUT othertest/{othertest}

Route Name: othertest.update

Action: OtherTestController@update


URI: PATCH othertest/{othertest}

Route Name: othertest.update (shares the name with the above)

Action: OtherTestController@update


URI: DELETE othertest/{othertest}

Route Name: othertest.destroy

Action: OtherTestController@destroy

Community
  • 1
  • 1
Meetarp
  • 2,331
  • 1
  • 23
  • 31
0

This method auto detect "GET", "POST", "PUT/PATCH", "DELETE" methods.

Route::resource()

This method auto detect argument from URL

Route::controller()

Also look it: Laravel 4 : Route to localhost/controller/action

Community
  • 1
  • 1
Adil
  • 1,008
  • 11
  • 21