Home>Article>PHP Framework> How does Laravel receive routing parameters and parameters in the query string at the same time?

How does Laravel receive routing parameters and parameters in the query string at the same time?

藏色散人
藏色散人 forward
2021-03-04 11:22:20 2545browse

The following tutorial column will introduce Laravel to receive routing parameters and parameters in the query string at the same time. I hope it will be helpful to friends in need!Laravel receives both route parameters and parameters in the query string

Laravel allows in the controller The method captures the parameters defined in the route, as follows:

The parameters defined in the route:

Route::get('post/{id}', 'PostController@content');


Capture routing parameters in the controller method:

class PostController extends Controller { public function content($id) { // } }
Laravel captures routing parameters and query string parameters at the same time

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 document

Dependency Injection & Route ParametersIf 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:

 public function detail(Request $request, $id, $source = '', $mediun = '', $campaign = '') { // }
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.

URL example:

http://example.com.cn/article/1/wx/h5?param1=val1¶m2=val2


In this example
"wx "will be passed to the variable$source
,"h5"will be passed to the variable$mediumRecommended:

The latest five Laravel video tutorials

The above is the detailed content of How does Laravel receive routing parameters and parameters in the query string at the same time?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete