14

I understand that a Resource controller can have the following methods

index
show
create
edit
store
update
destroy

Now suppose I have the following actions which need to be performed in addition to the resource actions:

  • User attempts to log in.
  • Admin wishes to find a user by email / first-name
  • User requests a post by it's slug

Are resource controllers useless for the above functionality? If programming an API, I obviously want the index, show, edit,create,destroy... but also the login, find, search etc...

Is it possible to route to both types of controller? e.g.

Route::group(['prefix' => 'api'], function() {
    Route::group(['prefix' => 'v1'], function() {
        // Resource Controller
        Route::resource('posts', 'Api\V1\PostsResourceController');

        // Restful Controller
        Route::controller('posts', 'Api\V1\PostsController');
    });
});

Or should I just forget about the resource controller and use a restful controller instead?

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
Gravy
  • 12,264
  • 26
  • 124
  • 193
  • I would use resource route only when i have CRUD actions to perform on that logic. Otherwise i use controller route. Resource one creates lots of routes to your app and if you don't use them it's kind of waste. – arma Apr 13 '14 at 13:44
  • @arma you can blacklist or whitelist those routes: http://laravel.com/docs/4.2/controllers#restful-resource-controllers – Nuno Rafael Figueiredo Jan 29 '15 at 17:03

2 Answers2

32

Just use a resource controller, add those other methods to that same controller, and add routes to those methods directly:

Route::group(['prefix' => 'api'], function()
{
    Route::group(['prefix' => 'v1', 'namespace' => 'Api\V1'], function()
    {
        // Add as many routes as you need...
        Route::post('login', 'PostsResourceController@login');
        Route::get('find',   'PostsResourceController@find');
        Route::get('search', 'PostsResourceController@search');

        Route::resource('posts', 'PostsResourceController');
    });
});

P.S. I generally shy away from using Route::controller(). It's too ambiguous.

Community
  • 1
  • 1
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • Thanks for this... I guess I was trying to take shortcuts by wanting to route to controller and a resource! – Gravy Sep 30 '13 at 20:28
  • Hi, i read article but don't exactly understand why it's too ambiguous. Can you explain? Why resource not abmiguous (resource create more routes and dont't check existence of action)? Why laravel has routes and controllers. Why not use only resource if it's better. This is confuses future artisans. Thanks – Sonique May 06 '14 at 23:56
  • 1
    @VitalyLarchenkov - **resource create more routes**. Nope. A resource creates only the routes you need (if you don't need them all, you should specify [`only` or `except`](http://i.imgur.com/NVIy57C.png), as outlined [in the docs](http://laravel.com/docs/controllers#resource-controllers). – Joseph Silber May 07 '14 at 03:12
  • Thank you. The `namespace` parameter is very useful. – Guicara May 29 '14 at 21:25
  • Resource is performance-expensive. Controller is better, more strategic and allows a single route declaration. – Mostafa Talebi Dec 07 '14 at 14:39
  • Late response: Controllers. You get a prettier routes file as it's like defining routes in the controller. Just remember to cache them in production, for speed - not that it's slow, but more speed is always preferred. – Lasse R Sep 16 '15 at 18:44
1

one of the problems associated with resource controllers are when you are using named routes, with group prefixes it all turns out into a big mess . if you want to make a small change in your prefix, you have to make changes throughout the views and controllers . ie you can't make full power of named routes.

i follow this model when developing my laravel apps .

Route::group( [ 'prefix' => 'admin' ], function(){
        Route::resource('pages', 'PageController', [
            'names' => [
                'show' => 'page',
                'edit' => 'page.edit'
            ],
            'only' => [
                'show',
                'edit'
            ]

        ]);

    });

so that i have the following advantages .

  • there are only routes that you need.
  • all the urls are clearly named

and i can generate urls comfortably using the syntax,even if i make a change in prefix or resource names urls are not affected

URL::route('page', array($id))
Sojan Jose
  • 3,168
  • 6
  • 33
  • 52