Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

opheliadesign's avatar

Any way to avoid entering controller class name in a Route::group()?

Hi everyone,

Is it possible to use a Route::group() without having to repeatedly specify a controller within it? For example,

Route::group(['prefix' => 'users', 'namespace' => 'Admin'], function() {
    // Everything here uses the /App/Controllers/Admin/UsersController
    get('index', ['as' => 'admin.users.index', 'uses' => '@index']);
});
0 likes
30 replies
xsmalbil@icloud.com's avatar

Like this?


Route::group(
    [
        'prefix'    => 'posts',
        'middleware'    => 'auth',
    ],
    function()
    {
        get('/',                 ['as'=>'posts.index',              'uses'=>'PostsController@index']);


    }
    
);

bashy's avatar

Is it just for lazyness or do you not want to type it all the time? Ease of changing it maybe?

Thought about using a variable or something?

Route::group(['prefix' => 'users', 'namespace' => 'Admin'], function() {
    $users_controller = 'UserController';
    get('index', ['as' => 'admin.users.index', 'uses' => "$users_controller@index"]);
});
1 like
opheliadesign's avatar

@bashy lazyness I guess? LOL ;) Everything that goes into a particular Route Group will use the same controller, so I was hoping to find a way to avoid repeating the class name of the controller every time - sort of how the namespace attribute works. The variable idea does not really save time as I'm still having to type the variable name, although code complete can help with that.

bashy's avatar

Yeah apart from that, I can't think of anything that will do it. Laravel doesn't support anything in the Route::group() for controller name does it? Would be handy though, guess it hasn't been done because the "uses" would need to account for it...

xsmalbil@icloud.com's avatar

@opheliadesign

What you could also do to make things even more lazy loving:


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

It will save you methods!!

  • edit - ... not saying that you are lazy btw..but you should be
1 like
opheliadesign's avatar

@xsmall D'oh I forgot all about Route::controller for some reason. I seem to recall reading somewhere that this can have a performance hit, is that true?

bashy's avatar

Route::controller reads the class to see what methods are available. They have to match getHome (/home - GET) etc. You can use that but you lose the named routes and being able to read them easily.

opheliadesign's avatar

@bashy Yep you read my mind, I prefer having named routes (grown to love it recently). So your variable idea might actually be the best, PhpStorm can quickly autocomplete that for me.

xsmalbil@icloud.com's avatar

@opheliadesign lookups are always more expensive then direct hits, but I'm not sure how much it will cost you. You'll need to benchmark. Try Autobench.

1 like
bashy's avatar

Also, you may want to look into Route::resource(). That does the exact same as that example route you put.

It generates multiple routes for one resource, you could have a resource for /users and it would generate

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

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

xsmalbil@icloud.com's avatar
Level 12

Route::controller( '/' , 'PostsController' , [
    'getIndex' => 'posts.index' ,
    'getCreate' => 'post.create'
] );

opheliadesign's avatar

@bashy I've been using RESTful controllers quite a bit lately but I need several other routes apart from the standard ones. I've been using a Route::controller() prior to Route::resource() to handle these additional routes, is that a good practice or can you suggest a better approach?

bashy's avatar

@xsmall Forgot you can do that but still icky!

Yeah I think resource is best to keep to. I then just use a normal Route::get() etc for any additional ones but I don't tend to have many others than a few. I also use controller for like /ajax.

opheliadesign's avatar

@bashy @xsmall Thank you both for your prompt replies and assistance! I'd love to suggest a correct answer here, suggestions? ;)

xsmalbil@icloud.com's avatar

@opheliadesign It depends on the context.

I use the Route::resource only for index, show, update, delete, store, and the controllers that I use as resource controllers mostly have similar names. PostsApiController, BeerApiController, DogsApiController...etc I extend them all from an BaseApiController, which has all the different response code that you might expect for resources.

xsmalbil@icloud.com's avatar

Then I have ViewControllers that only return views and ViewComposers that are called like the named routes.

Like HomeIndex.php, InfrastructureVmsNew.php etc...

bashy's avatar

@opheliadesign Not bothered, whatever helped you helped you :) can post your own answer if you want.

@xsmall Yeah sounds similar to what I do.

1 like
opheliadesign's avatar

What an awesome community Laracasts is :) So many great suggestions in a short period of time!

Thanks to @bashy and @xsmall I have learned the following -

What I was hoping to accomplish is not possible. However, using a variable to store the controller's class name is one approach -

Route::group(['prefix' => 'users', 'namespace' => 'Users'], function() {
    $controller = "UsersController";
    get('/', ['as' => 'admin.users.index', 'uses' => "$controller@index"]);
});

Or, I can use Route::controller() but at the expense of losing named routes.

Finally, I can use Route::resource() for the typical methods (store, update, destroy) and extend with Route::get() or a Route::controller()

I'm accepting my own answer to be fair to both participants... simply can't decide between all of the correct answers. Thanks for your help! :)

bashy's avatar

@opheliadesign as xsmall said on the first page, you can actually state the names for a controller route

Route::controller( '/posts', 'PostsController', [
    'getIndex' => 'posts.index' ,
    'getCreate' => 'posts.create'
]);
nathanrobjohn's avatar

You could always user Route::resource() and just place the extra routes you need under neath Route::resource('photo', 'PhotoController'); Route::get('ConfirmDestory', 'PhotoController@ConfirmDestory');

opheliadesign's avatar

@bashy I read that post again and it actually appears to be just what I was looking for - but you called it "icky," why? May accept it as the correct answer. :)

bashy's avatar

Still have to do the same amount of code? I don't know what size your controllers will be/are but I guess it's okay to use for that :P

opheliadesign's avatar

@bashy I like it because it feels more fluid to me as I code, just my preference. I also like the fact that I have both named routes AND the flexibility of controller routing.

In reality the controller I'm working with in this particular case is quite complex, so the general CRUD capabilities of Route::resource() really do not help, as I'm not dealing with one specific model. It's part of the back-end for a real time team deployment system, it'll be handling telecommunications, rostering, etc.

Changing selected answer.. :)

bashy's avatar

May want to extract that a little :P

opheliadesign's avatar

@bashy I'm putting everything in my project in the same controller, isn't that what you're supposed to do??

LOL yeah it's broken up into several controllers. Eventually Lumen will do most of the heavy lifting for this aspect of the project.

bashy's avatar

Okay cool :P thought for a second you were doing a whole project in one controller!

1 like

Please or to participate in this conversation.