将多个参数从laravel 5中的路径传递给控制器

时间:2017-02-26 09:56:44

标签: php laravel routes

我在

路线:

Route::get('feed/{type?}/{first?}/{second?}/{third?}', ['as' => 'feed', 'uses' => 'PostController@feed']);

控制器:

public function feed(Request $request, $type, $first, $second, $third)
{
...

但这会产生错误:

ErrorException in PostController.php line 209:
Missing argument 3 for App\Http\Controllers\PostController::feed()

我做得不好?我忘记了什么? 谢谢。

2 个答案:

答案 0 :(得分:2)

根据Laravel Docs

  

确保将路线的相应变量设为默认值

所以它应该是这样的:

public function feed(Request $request = null, $type = null, $first = null, $second = null, $third = null)
{
...

您可以使用您选择的默认值替换null

答案 1 :(得分:1)

你应该将参数声明为可选的

public function feed(Request $request, $type = '', $first = '', $second = '', $third = '')
{