Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 4: difference between resource and controller in Route class

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

Route::controller()

and

Route::resource()

thanks,

like image 593
mwafi Avatar asked Jun 12 '14 07:06

mwafi


3 Answers

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)
like image 116
mwafi Avatar answered Sep 28 '22 07:09

mwafi


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.

like image 29
ruudy Avatar answered Sep 28 '22 07:09

ruudy


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

like image 24
Mephoros Avatar answered Sep 28 '22 08:09

Mephoros