如何将多个参数传递给Laravel命名资源路由

时间:2019-03-25 10:16:53

标签: php laravel laravel-5 laravel-routing resource-router-middleware

我有一个命名的路线。 我想将两个参数传递给'edit'操作(例如{id}{month})。

我试图通过数组传递参数,但仍然无法正常工作。

Route::resource('admin/worktimes', 'WorktimesController')->names([
    'index' => 'worktimes',
    'show' => 'worktimes.show',
    'create' => 'worktimes.create',
    'edit' => 'worktimes.edit',
    'store' => 'worktimes.store',
    'update' => 'worktimes.update'
])

{{ route('admin/worktimes', array($id, $month) }}

创建的网址为“ http://.../admin/worktimes/4/edit?month=2019-05”。我想要类似“ http://.../admin/worktimes/4/2019-05/edit”的内容。

2 个答案:

答案 0 :(得分:0)

使用resource

无法获得理想的结果

制作Route('admin/worktimes/{id}/{month}/edit','WorktimesController@edit') 在您的控制器中,编辑方法将类似于 public function edit($id,$month){ //your code }

答案 1 :(得分:0)

默认资源方法不允许在编辑中使用多个参数。

它们是资源路由中自动生成的网址。

,如果我们需要更改这些,则必须更改laravel的某些核心路由功能。

那不是一个好主意。因为那会影响到项目的所有编辑路径。

所以我们只是使用路由规则覆盖资源编辑路由。

 Route::get('admin/worktimes/{id}/{month}/edit', ['as' => 'worktimes.edits', 'uses' => 'WorktimesController@edit']);

此规则必须写在route.php中为worktimesController写的资源路由之后。

谢谢

相关问题