Search code examples
phplaravelroutes

Using a slash in the beginning of the role makes any different?


Assume this route please:

Route::post('/role/make', 'roleController@make_role')->name('make_role');

As you see, there is a / in the beginning of route path. The interesting part is when I remove that /, still it works as expected.

So is there any different between route above and this?

Route::post('role/make', 'roleController@make_role')->name('make_role');

Solution

  • You can probably run php artisan route:list to confirm that it makes absolutely no difference in the resulting route in the routes lookup table, but if in doubt this is the source code that adds a route in 5.3. At some point the router calls prefix method on the Router which does:

    return trim(
            trim($this->getLastGroupPrefix(), '/')
            .'/'
            .trim($uri, '/'), 
     '/') ?: '/';
    

    That is to say it will trim both the URI and prefix to remove the / and then trim the result and do the same.

    I don't think this has changed in other versions.