Support the ongoing development of →
posted 10 years ago
Last updated 2 years ago.

I think what you are asking would be fixed by doing the following:

// Allow public profile page
Route::get('user/{name}', 'UserController@show');

// Allow user to edit their profile if they are logged in
Route::group(array('before' => 'auth'), function() {
    Route::get('user/{id}/edit', 'UserController@edit'));    
    Route::post('user', 'UserController@store');

// Allow only admin to create/delete profile by running a authAdmin filter before allowing access to url
Route::group(array('prefix' = > 'admin', 'before' => 'authAdmin'), function() {
    Route::get('user/{id}/delete', 'UserController@destroy');
    Route::get('user/create', 'UserController@create');

Using Route::resource or Route::controller and be easier to create at first, but you will lose some of the finer grained control of naming each route explicitly.

If you name your routes by using the array('as' => 'aliasName'), and you code your links using route('aliasName'), you change your urls. As long as the route names don't change, laravel automatically returns the new url.

Last updated 2 years ago.

I must be missing something but I do not see a solution in either of the examples. In the first I am forced to unwrap all the resource route definitions which is not an option because you need to define other HTTP request methods, not just GET and POST. In the second all I see is that the prefix is now propagated to where URL is referenced. This is not a solution to removing the prefix from the resource route names.

Neither one solves the problem of Laravel adding the URL prefix to the route name.

I have the same problem. I decided to move all low level resource manipulation to '/admin' prefixed urls and got all resource named routes now prefixed with 'admin.' which is not what I would expect or can make use of.

The whole idea of using named routes is to make them independent of URL's. Adding a prefix to the URL should not add it to the route name. The Route::group() has 'as' option for that.

For example:

Route::group(['middleware'=>'admin', 'prefix'=>'admin'], function () {
    Route::resource('products', 'ProductsController');

I would expect route('products.index') to be defined not route('admin.products.index')

If I want the latter, I would intuitively think that I need to do the following:

Route::group(['middleware'=>'admin', 'prefix'=>'admin', 'as' => 'admin.' ], function () {
    Route::resource('products', 'ProductsController');

This automatic prefixing of route names for resources not only makes named routes useless. It is a bug because adding both 'prefix' and 'as' to the group will result in doubling of the name prefix.

I cannot go through the whole application and modify the routes if I change the URL prefix. I might as well ignore the effing route names and use URL's throughout.

This is gave me tourette's, then I remembered that the beautiful thing about Laravel is that it is all customizable. The real solution requires overriding a single ResourceRegistrar method with my own so that the URL prefix in the group does not affect named routes.

Here is the ResourceNoPrefixRegistrar.php file, I put it into app\Providers;

namespace app\Providers;

use Illuminate\Routing\ResourceRegistrar;
use Illuminate\Routing\Router;

class ResourceNoPrefixRegistrar extends ResourceRegistrar
    public function __construct(Router $router)

     * Get the resource name for a grouped resource.
     * @param  string  $prefix
     * @param  string  $resource
     * @param  string  $method
     * @return string
    protected function getGroupResourceName($prefix, $resource, $method)
        // vsch: don't add prefix to the route name, if you want a prefix added to the route name add 'as' => 'prefix.' to the group options
        return trim("{$prefix}{$resource}.{$method}", '.');

Then you need to bind 'Illuminate\Routing\ResourceRegistrar' to the new class. Add the following to app\AppServiceProvider.php:

    public function register()
        \App::bind('Illuminate\Routing\ResourceRegistrar', function ()
            return \App::make('app\Providers\ResourceNoPrefixRegistrar');

Now I get what I want with full control: prefix only affects URLs, if I want a route name prefix I will add 'as' =>'prefix.' to the group options. Just remember the trailing dot, router will use the prefix as is when creating a named route.

Last updated 8 years ago.

I want to thank vsch for his response. This is the real solution and can save you a lot of time.


Thanks vsch! I just had the same exact problem and your solution is wonderful! This should be the default behaviour if you ask me.

Last updated 8 years ago.

@vsch Do you know when the prefixes are applied? Because I am getting the exact opposite... My route names are not passing the prefix and I have multiple routes with the same name now.


Sign in to participate in this thread!


Your banner here too?

wojt3k9 wojt3k9 Joined 21 Jun 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.