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

Laravel – Is it possible to pass the general route name as a route group?

I have the following api route:

GET /api/v1/users
POST /api/v1/users // middleware auth

In my api.php I have the following code for this:

Route::group(['prefix' => 'v1'], function() {
    require __DIR__ . '/v1/api/users.php';
});

In my v1/api/users.php I have this code:

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

Route::middleware('auth:sanctum')->group(function () {
    Route::post('/users', function (Request $request) {
        return [];
    })->name('create');
});

Route::get('/users', function (Request $request) {
    return [];
})->name('index');

My goal is to give the name a prefix users.. So that I can then the route name: users.delete or users.index as the name. Here my attempt: For this reason I tried to wrap the above code into a Route::group:

Route::group(['name' => 'users.'], function() {

    Route::middleware('auth:sanctum')->group(function () {

        Route::post('/users', function (Request $request) {
            return $request->user();
        })->name('delete');

        Route::get('/users', function (Request $request) {
            return [];
        })->name('index');

});

Problem However, Laravel then no longer recognises the wrapped routes. How can I rewrite this so that it works?

>Solution :

Route name prefixes are configured as follows:

Route::name('users.')->group(function () {
    Route::get('/users', function () {
        // Route assigned name "users.index"...
    })->name('index');
});
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