英文:
Change route parameter key in Laravel resource route definition for the only update method
问题
我使用资源路由如下:
Route::resource('article', \App\Http\Controllers\ArticleController::class);
然后我得到了这些路由:
dashboard.khassepaz.test/article/create
dashboard.khassepaz.test/article/inValidate/{article}
dashboard.khassepaz.test/article/validate/{article}
dashboard.khassepaz.test/article/{article}
dashboard.khassepaz.test/article/{article}
dashboard.khassepaz.test/article/{article}
dashboard.khassepaz.test/article/{article}/edit
我想只将更新方法的键更改为{dashboard_article_update}
,像这样:
dashboard.khassepaz.test/article/create
dashboard.khassepaz.test/article/inValidate/{article}
dashboard.khassepaz.test/article/validate/{article}
dashboard.khassepaz.test/article/{article}
dashboard.khassepaz.test/article/{dashboard_article_update} -------
dashboard.khassepaz.test/article/{article}
dashboard.khassepaz.test/article/{article}/edit
是否有办法只通过资源路由定义来实现这个目标?
我尝试过以下方法,但它不起作用:
Route::resource('article', \App\Http\Controllers\ArticleController::class, [
'update' => ['params' => ['article' => 'dashboard_article_show']]
]);
英文:
I use resource route like this
Route::resource('article', \App\Http\Controllers\ArticleController::class);
and i get these routes
dashboard.khassepaz.test/article/create
dashboard.khassepaz.test/article/inValidate/{article}
dashboard.khassepaz.test/article/validate/{article}
dashboard.khassepaz.test/article/{article}
dashboard.khassepaz.test/article/{article}
dashboard.khassepaz.test/article/{article}
dashboard.khassepaz.test/article/{article}/edit
I want to only change the update method key to {dashboard_article_update}
like this:
dashboard.khassepaz.test/article/create
dashboard.khassepaz.test/article/inValidate/{article}
dashboard.khassepaz.test/article/validate/{article}
dashboard.khassepaz.test/article/{article}
dashboard.khassepaz.test/article/{dashboard_article_update} -------
dashboard.khassepaz.test/article/{article}
dashboard.khassepaz.test/article/{article}/edit
is there any way to do that with just a resource route definition?
I tried this but it does not work.
Route::resource('article', \App\Http\Controllers\ArticleController::class, [
'update' => ['params' => ['article' => 'dashboard_article_show']]
]);
答案1
得分: 1
你可以利用Laravel的部分资源路由
在声明资源路由时,你可以指定控制器应处理的一部分操作,而不是默认操作的完整集合
Route::resource('article', ArticleController::class)->except([
'create', 'store', 'destroy', //...
]);
并创建一个专用的更新路由
Route::put('/article/{dashboard_article_show}', [ArticleController::class, 'update']);
英文:
You could take use of Laravel's Partial Resource Routes
When declaring a resource route, you may specify a subset of actions the controller should handle instead of the full set of default actions
Route::resource('article', ArticleController::class)->except([
'create', 'store', 'destroy', //...
]);
and create a dedicated update route
Route::put('/article/{dashboard_article_show}', [ArticleController::class, 'update']);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论