The parameters defined in the route:
Route::get('post/{id}', 'PostController@content');
Capture routing parameters in the controller method:
Laravel captures routing parameters and query string parameters at the same timeclass PostController extends Controller { public function content($id) { // } }
How can I capture both in the controller? The parameters defined in the route can also receive the parameters in the url query string. For example, the request link is like this
http://example.com.cn/post/1?from=index Quoting the explanation from the official website documentDependency Injection & Route Parameters
If your controller method is also expecting input from a route parameter you should list your route parameters after your other dependencies.
That is to say, if you want to still use the parameters in the route when the controller method injects dependencies, you need to list the parameters in the route after the method dependencies, for example:get('from') } }Laravel captures multiple optionals Parameters
In addition, we can also define multiple optional parameters in laravel routing:
Route::get('/article/{id}/{source?}/{medium ?}/{campaign?}', 'ArticleController@detail')Optional parameters in the controller method need to be defined as default parameters:
After defining this, route You can pass 0 to 3 optional parameters in the URL, but they must be in order: that is, if you want to pass the second optional parameter, the first optional parameter must be present. public function detail(Request $request, $id, $source = '', $mediun = '', $campaign = '') { // }
In this example
"wx "will be passed to the variable
$source
,"h5"
will be passed to the variable$medium
Recommended: