我正在使用
apiResource
和其他路线。我将它们分组如下:
Route::group(['prefix' => 'posts'], function () {
Route::group(['prefix' => '/{post}'], function () {
Route::put('lablabla', [PostController::class, 'lablabla']);
});
Route::apiResource('/', PostController::class, [
'names' => [
'store' => 'create_post',
'update' => 'edit_post',
]
]);
});
除了
index
和 store
之外的所有 apiResource 路由都不起作用!我该如何对路线进行分组?
您的路由语法是错误的,
注释
把它拿出来https://laravel.com/docs/8.x/controllers#restful-naming-resource-routes
应该是
Route::apiResource('posts', PostController::class)->names([
'store' => 'create_post',
'update' => 'edit_post',
]);
无需重复Route::group,你可以这样写你的路线
Route::prefix('posts')->group(function () {
Route::put('lablabla', [PostController::class, 'lablabla']);
});
Route::apiResource('posts', PostController::class)->names([
'store' => 'create_post',
'update' => 'edit_post',
]);
你的语法不正确,有一个名称方法。请参阅此处的文档https://laravel.com/docs/8.x/controllers#restful-naming-resource-routes。