Framework: [5.3] Router "prefix" doesn't prepend the route name + another problem

Created on 29 Aug 2016  路  7Comments  路  Source: laravel/framework

Hi,

So in laravel 5.2 when i do this

Route::group(['prefix' => 'admin'], function ()
{
    Route::resource('articles/categories', 'ArticleCategoriesController');
});

The prefix will have two effects :

1- the url will be admin/articles/categories
2- the route name will be admin.articles.categories

But in 5.3

There is two problems.
1- the "admin." doesn't get added to the route name and to solve that i had to use 'as' => 'admin.' on the same route group where i define the prefix

2- This is not related to the prefix, but in 5.2 the "articles/" in the resource url will result in having "articles." in the route name. To solve this problem i had to do this

Route::group(['prefix' => 'admin'], function ()
{
    Route::resource('articles/categories', 'ArticleCategoriesController', [
        'as' => 'articles'
    ]);
});

Now what is wierd is that the resource route names will be like

admin.articles.categories

instead of

admin.articles

Shouldn't the 'as' value be used to generate the resource route names when the 'as' is present on the resource, instead of appending the ".categories" from the url ?

Most helpful comment

The name property can be used to specify a name to a prefix, though it still needs the dot to be added manually at the end:

Route::prefix('my-users')->name('my-users.')->group(function() {
    Route::get('the-admin', 'GetTheAdmin@admin')->name('the-admin');
    Route::prefix('some-stats')->name('some-stats.')->group(function() {
        Route::get('chart', 'GetTheAdmin@chart')->name('chart');
        Route::get('graphs', 'GetTheAdmin@graphs')->name('graphs');
    });
});

will produce the following routes:

route: my-users/the-admin | name: my-users.the-admin 
route: my-users/some-stats/chart | name: my-users.some-stats.chart
route: my-users/some-stats/graphs | name: my-users.some-stats.graphs

All 7 comments

Thanks. This was intentionally changed in 5.3 due to popular demand for changing this.

@shadoWalker89
Change to

Route::resource('articles/categories', [
        'as' => 'articles',
        'uses' =>  'ArticleCategoriesController'
    ]);

//or

Route::resource('articles/categories', 'ArticleCategoriesController')->name('articles');

Also the prefix thing is because if you wanted to prefix it with '/dashboard' but in your routes call it as 'admin' it would append both the prefix and the as value generating dashboard.articles.admin.articles that's why this changed.

@GrahamCampbell I understand why this was changed but is there another way to add a prefix to the route names inside a group or does it have to be done manually on each route now?

If it has to be done manually, and we can't use route resources inside groups due to route names collisions, it would seem like a poorly thought out fix because those were two useful features.

Would it be possible to add a route name prefix parameter for groups?

GrahamCampbell, Well, I really feel much has been made that change. There is no possibility that follow incorporated this quality and who do not want to deactivate. Thank you

Had a similar issue and the workaround advised on https://github.com/laravel/internals/issues/181 seems acceptable for me. Maybe try giving it a shot too.

@yajra I agree that it's an acceptable workaround, however the fact that you need to leave the dot makes it seems like it's not an intentional feature like someone else commented, which makes me worried that they'll just remove it in a future release because "it's not how you're supposed to do it".

The name property can be used to specify a name to a prefix, though it still needs the dot to be added manually at the end:

Route::prefix('my-users')->name('my-users.')->group(function() {
    Route::get('the-admin', 'GetTheAdmin@admin')->name('the-admin');
    Route::prefix('some-stats')->name('some-stats.')->group(function() {
        Route::get('chart', 'GetTheAdmin@chart')->name('chart');
        Route::get('graphs', 'GetTheAdmin@graphs')->name('graphs');
    });
});

will produce the following routes:

route: my-users/the-admin | name: my-users.the-admin 
route: my-users/some-stats/chart | name: my-users.some-stats.chart
route: my-users/some-stats/graphs | name: my-users.some-stats.graphs
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Xerotherm1c picture Xerotherm1c  路  70Comments

mstnorris picture mstnorris  路  87Comments

rafaelrenanpacheco picture rafaelrenanpacheco  路  64Comments

thewinterwind picture thewinterwind  路  63Comments

nkeena picture nkeena  路  75Comments