Summary of TP5 routing knowledge

I. routing mode

Normal mode (default pathinfo, no route resolution)

url_route_on' => false

Mixed mode (pathinfo + resolve route)
'url_route_on' => true,
'url_route_must'=> false,
Forced mode (must route)
url_route_on' => true,
'url_route_must' => true,

2. Dynamic registration route

1. Dynamic registration route

Route::rule('routing expression ',' routing address', 'request type', 'routing parameter (array)', 'variable rule (array));

Example
return [
Route::rule('demo/:name/[:sex]', 'index/Index/demo', 'GET', ['ext' => 'html'], ['name' => '\w+', 'age' => '\w{1,2}']),
]

Routing expression: demo/:name/[:sex] ("[]" is optional)
Routing address / index / demo
Request type: get|post |, etc.
Routing parameters: ['ext '= > html' (suffix detection), etc.
Variable rule: ['sex '= > 0| 1' (only 0 and 1), etc., regular expression

//Using closures in the root path
Route::get('/', function () {
    echo 'I am demo3,Temporary maintenance, thank you for your support!';
}),

//301 redirection
//     Route::get('demo4', 'http://www.baidu.com'),
2. Dynamic registration route in array mode (concise)
return [
//pattern global variable
'__pattern__' => [
    'name' => '\w+',
    ...
],

'demo2' => 'admin/Index/demo2',
'demo3/:name/:age' => ['admin/Index/demo3', ['method' => 'get', 'ext' => 'asp'], ['age' => '\d+']],
'demo4/:name/:money' => ['admin/Index/demo4', ['method' => 'get', 'ext' => 'asp'], ['money' => '\d+']],
'demo5/:name' => ['admin/Index/demo5', ['method' => 'get', 'ext' => 'php'], []],
];

3. Route grouping

Individual: the same route definition (demo) matches different variable rules according to different values of URL delivery parameters, so as to enter different route rules. For example, the following

Item by item

'demo/:num' => ['admin/Index/demo1', ['method' => 'get'], ['num' => '\d{2,4}']],
'demo/:str' => ['admin/Index/demo2', ['method' => 'get'], ['str' => '[a-zA-Z]+']],
'demo/:bool' => ['admin/Index/demo3', ['method' => 'get'], ['bool' => '0|1']],

II. Merger

'[demo]' => [
    ':num' => ['admin/Index/demo1', ['method' => 'get'], ['num' => '\d{2,4}']],
    ':str' => ['admin/Index/demo2', ['method' => 'get'], ['str' => '[a-zA-Z]+']],
    ':bool' => ['admin/Index/demo3', ['method' => 'get'], ['bool' => '0|1']],
],

③ group registration

//Route::group('route expression ',' definition of route ');
think\Route::group('demo', [
    ':num' => ['admin/Index/demo1', ['method' => 'get'], ['num' => '\d{2,4}']],
    ':str' => ['admin/Index/demo2', ['method' => 'get'], ['str' => '[a-zA-Z]+']],
    ':bool' => ['admin/Index/demo3', ['method' => 'get'], ['bool' => '0|1']],
]);

Keywords: PHP

Added by mdemetri2 on Fri, 25 Oct 2019 18:19:02 +0300