Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Example:

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')
    });
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading