framework: [5.3] (Revert) Resource Route Names No Longer Affected By Prefixes

Given the changes above, how would you group your resource routes on Laravel 5.3?

For instance, I have an application that have frontend & backend panel with same route resource? In 5.2:

// Backend post resource
$router->group([
    'middleware' => 'administrator',
    'prefix'     => 'administrator',
], function ($router) {
    $router->resource('posts', PostController::class);
});

// Frontend post resource 
$router->resource('posts', PostController::class);

This will yield a better grouping on resource route name.

administrator.posts.index  => /administrator/posts
administrator.posts.create => /administrator/posts/create
...
posts.index => /posts
posts.create => /posts/create
...

But on 5.3, same route will produce duplicate names.

posts.index  => /administrator/posts
posts.create => /administrator/posts/create
...
posts.index => /posts
posts.create => /posts/create
...

Hence if you use route() helper then your upgrade path to 5.3 is doomed!

I know that we can fix it by manually naming the routes on administrator side (which is what I did on my project) but considering a big project, IMHO, too much code duplications! Imagine that we have to manually write all our administrator resource routes?

$router->get('posts', PostsController::class . '@index')->name('administrator.posts.index');
$router->get('posts/create', PostsController::class . '@create')->name('administrator.posts.create');
$router->post('posts', PostsController::class . '@store')->name('administrator.posts.store');
$router->get('posts/{posts}/edit', PostsController::class . '@edit')->name('administrator.posts.edit');
$router->put('posts/{posts}', PostsController::class . '@update')->name('administrator.posts.update');
$router->delete('posts/{posts}', PostsController::class . '@destroy')->name('administrator.posts.destroy');

I hope this can be reconsidered. Or if there is better way that I am missing, please enlighten me. Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 22 (7 by maintainers)

Most upvoted comments

@crynobone changing from prefix to domain use case makes perfect sense. Didn’t think of that.

@RDelorier suggested a workaround to use as https://github.com/laravel/internals/issues/181 and would give it a shot.

Route::group([
    'as' => 'admin.', // the trailing dot makes me think this is unintended
    'prefix' => 'admin'
], function () {
    Route::resource('users', 'UserController');
});

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

Thanks for the feedbacks!

This worked for me :

// Backend post resource $router->group([‘middleware’ => ‘administrator’, ‘prefix’ => ‘administrator’], function ($router) { $router->resource(‘posts’, ‘PostController’, [‘as’ => ‘administrator’]); });

// Frontend post resource $router->resource(‘posts’, ‘PostController’);

thanks

People were using both actually, there’s a lot of issues related to this previous behavior, you can check some of them:

#12216 , #13903

#14547 , #14833

So basically prefix will affect the generation of your url meanwhile as will affect the name.