【问题标题】:Laravel restful routes contain parametersLaravel restful 路由包含参数
【发布时间】:2017-04-26 12:48:23
【问题描述】:

在我的 Laravel 路由文件 (web.php) 中,我有以下路由。

Route::resource('notifications/{section_id}', 'NotificationController', ['as' => 'notifications']);

然后我有一个刀片模板,可以打开这样的表单

{!! Form::model($qList,['route' => ['notifications.store']]) !!}

但是,此行会生成错误消息Route [notifications.store] not defined.

运行php artisan route:list 可以看到路由名称是notifications.{section_id}.store。即{section_id} 包含在路由名称中。这是我的问题吗?该参数似乎没有出现在我的任何其他路由中,并且我的 NotificationController 中确实有一个 store 方法

谢谢

【问题讨论】:

  • 再次尝试删除 {section_id} 参数和 route:list。
  • 这确实有效并显示了正确的路线,但我需要将参数传递给我的控制器。 {section_id} 是必需的
  • 将 $request->input('section_id') 转储到 store 方法中。不要忘记在方法实现中添加 Request $request 作为参数
  • 店铺路线现在怎么样了?从 route:list 输出粘贴它。
  • 但这是我的问题。我的 NotificationsController 中确实有一个 store 方法,但错误消息是该方法不存在

标签: laravel routes


【解决方案1】:

这样写,写完整的路由名称,并在路由数组中传递section_id参数:

Form::model($qList,['route' => ['notifications.{section_id}.store', $section_id]])

然后,表单提交 url 将变为(假设 99 作为您的部分 id)

<form method="POST" action="http://laravel5.dev/notifications/99" accept-charset="UTF-8"><input name="_token" type="hidden" value="...">

此外,您可以选择使用 'names' 参数在 RESTful 路由上强制路由名称,如下所示:

Route::resource('notifications/{section_id}', 'TestController', ['as' => 'notifications', 'names' => ['store' => 'notifications.store']]);

然后,php artisan route:list 你会看到

| Domain | Method                         | URI                                            | Name                                     | Action                                      | Middleware   |
+--------+--------------------------------+------------------------------------------------+------------------------------------------+---------------------------------------------+--------------+
|        | POST                           | notifications/{section_id}                     | notifications.store                      | App\Http\Controllers\TestController@store   | web          |

【讨论】:

  • 这引导我朝着正确的方向前进。感谢您的帮助
猜你喜欢
  • 2014-03-08
  • 2013-05-22
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-05
  • 2021-02-04
  • 2014-01-24
相关资源
最近更新 更多