anon
anon

Reputation:

Laravel 4 - resource controller adding group prefix without changing route names

See this example:

    Route::group(array('prefix' => 'admin'), function()
    {
        //this resolves to admin/departments
        Route::resource('departments', 'DepartmentsAdminController');
        //this resolves to admin/users
        Route::resource('users', 'UsersAdminController');
        //this resolves to admin/companies  
        Route::resource('companies', 'CompaniesAdminController');
        Route::resource('projects', 'ProjectsAdminController');
        Route::resource('jobs', 'JobsAdminController');
        Route::resource('milestones', 'MilestonesAdminController');
        Route::resource('hours', 'HoursAdminController');
        Route::resource('notes', 'NotesAdminController');
        Route::resource('briefs', 'BriefsAdminController');
        Route::resource('brief_items', 'BriefItemsAdminController');
    });

Laravel changes route names based on the prefix in this case the prefix is admin

so all route names now prefixed with admin see:

    admin.users.create
    admin.users.edit

But what if i want to change the prefix to something else I will have to change the route names in my entire application.

What i want is to keep route name as is

    users.create
    users.edit        

and change the prefix without changing the route name.

Is there a way to keep the route names in resource controllers static and change the prefix anytime i want ?

Upvotes: 3

Views: 4890

Answers (3)

Sojan Jose
Sojan Jose

Reputation: 3238

you can follow the given code sample i use . by following this model, you can define route names as you prefer.

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

        ]);

    });

and then you can generate urls using .

URL::route('page', array($id))

for generating urls to /admin/page/{$id}

Upvotes: 1

fideloper
fideloper

Reputation: 12293

See my Laravel blog code, where I do just that based on a configuration variable.

I'm not sure that will, however, fix your issue of naming the route. You should be able to use the "as" array method to name the route something static.

$adminGroup = Config::get('admin.group');
Route::group(array('prefix' => $adminGroup, 'as'=>'something'), function() { ... });

Note: I haven't confirmed that you can name a route group (vs a specific route within it). LMK how it works for you.

Lastly, I used a configuration variable, but there's nothing stopping that form being database driven.

Upvotes: 0

Hao Luo
Hao Luo

Reputation: 1891

I think what you are asking is not very reasonable... so you want named routes in a group route. So say IF laravel allows you set named group routes like: Route::group(array('prefix' => 'admin', 'as'=>'something'), function() {... and you can always do Redirect::route('something/users'). Then prefix option serves absolutely no purpose, so why not just use prefix.

Having said that, you could just use the action helper function to return the url as such: action('NotesAdminController@index')

Upvotes: 0

Related Questions