简体   繁体   中英

Pass multiple parameters to controller from route in laravel 5

I have in

routes:

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

controller:

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

But this produce error:

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

What I am doing bad? What I forgot? Thank you.

According to the Laravel Docs

Make sure to give the route's corresponding variable a default value

So it should be like this:

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

You can replace null with default value of your choice.

You should declare the arguments as optional too like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM