How much do you know about routing parameters?



Routing parameters

Routing grouping and rule definitions support specified routing parameters. These parameters mainly complete route matching detection and subsequent behavior.

Routing parameters can be passed in directly (in batches) when defining routing rules. It is recommended to use the method to configure the configuration more clearly.


##ParametersDescriptionMethod nameextURL suffix detection, supports matching multiple suffixesextdeny_ext URL prohibits suffix detection and supports matching multiple suffixesdenyExthttpsDetect whether https requesthttpsdomainDomain name detectiondomain##complete_matchmodelcacheajaxpjax##jsonJSON detectionjsonvalidateBind the validator class for data verificationvalidateappendAppend Additional parametersappendmiddlewareRegister routing middlewaremiddlewarefilterRequest variable filteringfilter
Whether the route is completely matchedcompleteMatch
Binding modelmodel
Request cachecache
Ajax detectionajax
Pjax detectionpjax

Usage example:

Route::get('new/:id', 'News/read')
    ->ext('html')
    ->https();

These routing parameters can be mixed. As long as any parameter fails to pass the check, the current route will not take effect and subsequent routing rules will continue to be detected.

If you need to set routing parameters in batches, you can also use the option method.

Route::get('new/:id', 'News/read')
    ->option([
        'ext'   => 'html',
        'https' => true
    ]);

URL suffix

If the URL suffix is ​​globally unified, you can set the url_html_suffix parameter in the application configuration file route.php. If the currently accessed URL The URL suffix in the address is a permitted pseudo-static suffix, so the suffix itself will not be passed in as a parameter value.

The differences between different parameter settings are as follows:


##Configuration valueDescriptionfalseAllow any pseudo-static suffixhtmlAllow multiple pseudo-static suffixes
Prohibit pseudo-static accessEmpty string
Only allow set pseudo-static suffixhtml|htm


// 定义GET请求路由规则 并设置URL后缀为html的时候有效
Route::get('new/:id', 'News/read')
    ->ext('html');

Supports matching multiple suffixes, for example:

Route::get('new/:id', 'News/read')
    ->ext('shtml|html');

If the ext method does not pass in any value, it means that no suffix access is allowed.

You can set a URL suffix that is prohibited from access, for example:

// 定义GET请求路由规则 并设置禁止URL后缀为png、jpg和gif的访问
Route::get('new/:id', 'News/read')
    ->denyExt('jpg|png|gif');

If the denyExt method does not pass in any value, it means that the suffix must be used for access.

Domain name detection

Supports using complete domain name or subdomain name for detection, for example:

// 完整域名检测 只在news.thinkphp.cn访问时路由有效
Route::get('new/:id', 'News/read')
    ->domain('news.thinkphp.cn');
// 子域名检测
Route::get('new/:id', 'News/read')
    ->domain('news');

If you need to define batches for subdomain names For routing rules, it is recommended to use the domain method for routing definition.

HTTPS detection

Support detection of current HTTPS access

// 必须使用HTTPS访问
Route::get('new/:id', 'News/read')
    ->https();

AJAX/PJAX/JSON detection

Can detect whether the current request is AJAX/PJAX/JSON.

// 必须是JSON请求访问
Route::get('new/:id', 'News/read')
    ->json();

Request variable detection

In addition to matching the routing address, you can additionally check whether the request variables match, only when the specified request variables are also consistent. to match this route.

// 检查type变量
Route::post('new/:id', 'News/save')
    ->filter('type', 1);   
    
// 检查多个请求变量
Route::post('new/:id', 'News/save')
    ->filter([ 'type' => 1,'status'=> 1 ]);

Append additional parameters

You can implicitly append additional parameters when defining the route. These parameters will not appear in the URL address.

Route::get('blog/:id', 'Blog/read')
    ->append(['app_id' => 1, 'status' => 1]);

When routing a request, two parameters, app_id and status, will be passed in at the same time.

Route binding model

Routing rules and grouping support binding model data, for example:

Route::get('hello/:id', 'index/hello')
    ->model('id', '\app\index\model\User');

will automatically bind the current route Set id as the User model data of the current routing variable value.

If your model binding uses id as the query condition, it can also be simplified to the following method

Route::get('hello/:id', 'index/hello')
    ->model('\app\index\model\User');

By default, if the model data is not queried, it will be thrown Exception, if you do not want to throw an exception, you can use

Route::rule('hello/:id', 'index/hello')
    ->model('id', '\app\index\model\User', false);

to define query conditions for model data, for example:

Route::rule('hello/:name/:id', 'index/hello')
    ->model('id&name', '\app\index\model\User');

indicates that the query id and name values ​​are equal to the model data of the current routing variable.

You can also use closures to customize the required model objects to be returned

Route::rule('hello/:id', 'index/hello')
    ->model(function ($id) {
        $model = new \app\index\model\User;
        return $model->where('id', $id)->find();
    });

The parameters of the closure function are the URL variable information of the current request.

The bound model can be automatically injected directly in the controller's architecture method or operation method. For details, please refer to the dependency injection in the request chapter.

Request caching

You can request cache processing for the current routing request, for example:

Route::get('new/:name$', 'News/read')
    ->cache(3600);

means caching 3600 for the current routing request seconds. For more information, please refer to the Request Caching section.

Routing middleware

You can use routing middleware, the registration method is as follows:

Route::rule('hello/:name','hello')
	->middleware('Auth');

Or register the middleware for routing group

Route::group('hello', function(){
	Route::rule('hello/:name','hello');
})->middleware('Auth');

If you need to pass additional parameters to the middleware, you can use

Route::rule('hello/:name','hello')
	->middleware('Auth:admin');

If you use constant definition, you can pass in the middleware parameters in the second parameter.

Route::rule('hello/:name','hello')
	->middleware(Auth::class, 'admin');

If you need to define multiple middlewares, use the array method

Route::rule('hello/:name','hello')
	->middleware([Auth::class, 'Check']);

You can pass in the same additional parameter

Route::rule('hello/:name','hello')
	->middleware([Auth::class, 'Check'], 'admin');

or specify the middleware parameters individually.

Route::rule('hello/:name','hello')
	->middleware(['Auth:admin', 'Check:editor']);

Dynamic parameters

If you need to customize some additional routing parameters, you can use the following method:

Route::get('new/:name$', 'News/read')
    ->option('rule','admin');

Or use the dynamic method

Route::get('new/:name$', 'News/read')
    ->rule('admin');

After subsequent routing actions, you can call the rule parameter of the route to perform permission check.