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:
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?
2
Answers
Route name prefixes are configured as follows:
Base on your Routes you can chain the
name()
after themiddleware()
:You can also use only the
group()
: