The parameters defined in the route:
Route::get('post/{id}', 'PostController@content');
Capture routing parameters in the controller method: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">class PostController extends Controller
{
public function content($id)
{
//
}
}</pre><div class="contentsignin">Copy after login</div></div>
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 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:Laravel captures multiple optionals Parameters<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class PostController extends Controller { public function content(Request $request, $id) { $from = $request->get('from') } }Copy after login
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: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"> public function detail(Request $request, $id, $source = '', $mediun = '', $campaign = '')
{
//
}</pre><div class="contentsignin">Copy after login</div></div>
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.
In this example
"wx " will be passed to the variable
$source
, "h5"
will be passed to the variable $medium
Recommended: