【问题标题】:How to pass multiple parameters to laravel named resource route如何将多个参数传递给 laravel 命名资源路由
【发布时间】:2019-08-15 13:25:29
【问题描述】:

我有一条命名路线。 我想将两个参数传递给 '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'之类的东西。

【问题讨论】:

  • 这是完整的路由定义吗?它是否包含有关映射变量的更多信息?乍一看,您错过了参数数组中的键

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


【解决方案1】:

resource 无法获得您想要的结果

制作Route('admin/worktimes/{id}/{month}/edit','WorktimesController@edit') 在你的控制器编辑方法中会像 public function edit($id,$month){ //your code }

【讨论】:

    【解决方案2】:

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

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

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

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

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

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

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

    谢谢

    【讨论】:

      猜你喜欢
      • 2019-12-07
      • 2015-01-25
      • 2018-06-17
      • 2021-10-15
      • 2018-06-20
      • 2019-03-03
      • 2018-08-14
      • 2016-07-01
      • 2016-09-19
      相关资源
      最近更新 更多