如何将 apiResource 路由和其他路由组合在一起?
how group apiResource route and other routes together?
我正在使用 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。
您的路由语法错误,
备注
- 您将为 apiResource(复数)
提供 uri
- 例如。 Route::apiResource('posts', PostController::class);
- 你的资源路由名称错误
把这个弄出来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',
]);
我正在使用 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。
您的路由语法错误,
备注
- 您将为 apiResource(复数) 提供 uri
- 例如。 Route::apiResource('posts', PostController::class);
- 你的资源路由名称错误
把这个弄出来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', ]);