Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter to implicit controller in Laravel 5?

In both laravel 4.2 and laravel 5.3, there is an option in route to use implicit controller. The syntax are the same for both version.

Route::controller('myroute', 'myDearController');

So the URL will be:

http://my.domain.com/myroute/function-name/parameter1/parameter2

In laravel 4, the controller will look like:

//..... Some other controller related syntax ......
public function getFunctionName($parameter1, $parameter2) {
    $inputs = Input::all();
    dd($inputs);
}
//..... Some other controller related syntax ......

However, in laravel 5, to get the GET input, it takes up a parameter place, so I tried to make something like this:

//..... Some other controller related syntax ......
public function getFunctionName(Request $request, $parameter1, $parameter2) {
    $inputs = $request->all();
    dd($inputs);
}
//..... Some other controller related syntax ......

However, the URL returns The site can't be reached. I tried switching the position,

//..... Some other controller related syntax ......
public function getFunctionName( $parameter1, $parameter2, Request $request) {
    $inputs = $request->all();
    dd($inputs);
}
//..... Some other controller related syntax ......

It does not work. I know I can add the line Route::get('myroute/function-name/{$parameter1}/{$parameter2}', 'myDearController@getFunctionName') to the route file, but besides explicitly specify in the route file, is there a default way to do so?

like image 691
cytsunny Avatar asked Oct 25 '16 06:10

cytsunny


1 Answers

With explicit routes, it should be {parameter} instead of {$parameter}. so,

Route::get('myroute/function-name/{parameter1}/{parameter2}', 'myDearController@getFunctionName')
like image 93
Sanzeeb Aryal Avatar answered Nov 14 '22 22:11

Sanzeeb Aryal