slovenianGooner's avatar

The same route name regardless of prefix

So I have a bunch of routes in a prefix group like so:

Route::group(['before' => 'core_auth', 'prefix' => Config::get('core::core.prefix')], function(){

    Route::get('/', ['as' => 'core.home', 'uses' => 'Ngine\Core\Controllers\Admin\Home@showHomepage']);
    // Elements routes
    Route::resource('elements', 'Ngine\Core\Controllers\Admin\Elements');

});

The naming of the routes then varies based on the prefix. How could I have the same naming of the routes regardless of the prefix of the group. This mainly goes for Route::resource.

For example the name of a resource route could be admin.elements.index or only elements.index if I lose the prefix.

Any ideas?

0 likes
13 replies
bashy's avatar

It doesn't do that already? If you have a prefix of admin, the route name would be admin.elements.index

Print your routes so far?

slovenianGooner's avatar

Yes, that is exactly what it does and that is exactly what I'm trying to avoid. I want to link to my routes in Blade in the same way, regardless of the prefix. I used to create a helper function that prepended the prefix, but was hoping for a more natural solution.

bashy's avatar

Why don't you display what routes names you want and how you want to link to them because I still don't fully understand.

P.s if you want to name the routes yourself, write them out manually and don't use route::resource?

RhythmScout's avatar

I'm in the same boat as the op....I have multiple prefixes and want to remove the prefix from the route name. ie., 'manager.staff.index' I want named as 'staff.index' (manager is my prefix). The main problem is with resource routes. I've found that I can rename routes as below, but this seems tedious. Can I use wildcards in route renaming?

Route::group(['prefix' => 'manager', 'before' => 'admin'], function()
{
    Route::resource('staff', 'StaffController', ['names' => [
        'index' => 'staff.index',
        'create' => 'staff.create',
        'store' => 'staff.store',
        'show' => 'staff.show',
        'edit' => 'staff.edit',
        'update' => 'staff.update',
        'destroy' => 'staff.destroy'
    ]]);
});

Am I approaching this wrong?

@slovenianGooner did you come to a solution?

bashy's avatar

Is there a massive issue with using manager.staff.* instead of staff.*?

slovenianGooner's avatar

@bashy The issue was when the routes are not always prefixed. For example, my app could be an admin section of a website or a completely standalone app, meaning if it's the admin section it had a prefix "admin" in the config, while as a standalone app there was no prefix, which was messed up by the Route::resource.

bashy's avatar

Yeah I know why you needed it, was just wondering since @RhythmScout didn't have a dynamic prefix

RhythmScout's avatar

@bashy, my issue is that I have multiple prefixes, 'manager', 'admin', 'dev' and many use the same resource. So essentially, I have named routes manager.staff.index, admin.staff.index, dev.staff.index all pointing to the same controller@action.

I can deal with it the way it is (I'm dynamically calling named routes by prepending the role name in my views). I just thought it would be cleaner if the same controller@action was always represented by a consistent route name.

bashy's avatar

Well, depends on your app but if you have multiple routes going to the same action but different URIs, you'd want different names anyway since they're separate?

If you dynamically call the route name in the views (or similar), it will always be correct depending on what URL you're on?

Like the OP, they use Config::get('core::core.prefix') to get the prefix. Wouldn't you just call that when getting the route name?

route(Config::get('core::core.prefix') . '.elements.index')
RhythmScout's avatar

Makes sense keeping the names/URIs separate. OP's solution is very similar to what I ended up doing when calling my routes:

route( Auth::user()->role->name . '.staff.index' )

Sticking with this method now. Thx for the input.

slovenianGooner's avatar

Your solution looks OK @RhythmScout, but imagine you want to change the way you process this in the future. Setting one config variable which you can refer to in your route method is much more open to changes than your way. Not saying you have to do it like that, but I've been burned in the past :)

RhythmScout's avatar

Great point. Taking your advice and changing this up now.

Please or to participate in this conversation.