How to Make Route Name Prefix Exclude No-Name Route?


I have an overriden Laravel Fortify routes, where I made one group of routes for Admin, and the other for User. I don’t want to define "/admin/" for the url and "admin." for the name of every single routes because that’s just madness. So after searching, I found out that you can use Prefix and Route Name Prefix by using something like this:

Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
    // some codes

While this works fine, the problem is it even create Route Name Prefixes for routes that don’t have name. For example, to login I have 2 routes. One is for displaying the login page (GET), and the other is POST request for the login itself. I gave the GET route a name called "login", but I don’t give the POST route a name.

When I check it with "php artisan route:list", the GET request become "admin/login" with name "admin.login", but the POST request become "admin/login" with name "admin.". This is the wrong part that I don’t want it. I want to exclude the Route Name Prefix from route that don’t have name in the first place. How can I do that? Thank you.

>Solution :

Laravel allow you split those routes that has named to another group.


Route::prefix('admin')->group(function () {
    // Put those routes here that you don't want to add name prefix.
    // Route::post('/login')

    Route::name('admin.')->group(function () {
        // Those routes you want to add name prefix.
        // Route::get('/login')

Leave a ReplyCancel reply