在Laravel资源路由定义中,只更改更新方法的路由参数键。

huangapple go评论79阅读模式
英文:

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']);

huangapple
  • 本文由 发表于 2023年5月28日 16:31:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定