Home > PHP Framework > Laravel > body text

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

藏色散人
Release: 2021-03-05 09:03:06
forward
2567 people have browsed it

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: <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 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:

<?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
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: <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.

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 $medium Recommended:

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!