thinkPHP5 common knowledge framework routing point summary

First, routing mode

Normal mode (default pathinfo, does not resolve routing)

'url_route_on' => false

Mixed mode (pathinfo + associated routing)

'url_route_on' => true,
'url_route_must'=> false,

Forced mode (must be routed)

'url_route_on' => true,
'url_route_must' => true,

Second, dynamic routing registration

1, dynamic routing registration

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

Examples

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

Routing expressions: demo /: name / [: sex] ( "[]" is optional)
the routing address: / index / Index / demo
Request Type: get | post | other
routing parameters: [ 'ext' => ' html '] (suffix detection), etc.
variable rules: [' sex '=>' 0 | 1 '] ( only 0 and 1) etc., the regular expression

// use the root path closures 
the Route :: GET ( '/', function () { 
 echo 'I Demo3, temporary maintenance, Thank you! "; 
}) 
// 301 redirects 
// Route :: get ( 'demo4', 'http://www.baidu.com') ,

2, an array of dynamically routing registration (simple)

return [
//pattern全局变量
'__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'], []],
];

Third, routing packets

Individual: same route definition (Demo), according to the URL transmission parameters different values, and different matching rules variables to enter different routing rules. For example,

① one by one

'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']],

② 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']],
],

③ registration packet using the group

// Route :: group ( 'routing expression', 'defined route'); 
Think \ :: the Route Group ( 'Demo', [ 
 ': NUM' => [ 'ADMIN / Index / the demo1', [ ' Method '=>' GET '], [' NUM '=>' \ D {2,4} ']], 
 ': STR '=> [' ADMIN / Index / demo2 ', [' Method '=>' GET '], [' STR '=>' [A-zA-the Z] + ']], 
 ': BOOL '=> [' ADMIN / Index / Demo3 ', [' Method '=>' GET '], [' BOOL '=>' 0 |. 1 ']], 
]);

④ extracting common portions from ③ - simplification

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

⑤ routing packets nested closure

Personal: Function and routing ④ basically the same, if not accustomed to using any special needs or closures, less or no bar

Route::group(['method'=>'get','ext'=>'html'],function(){
  Route::group('blog',function(){
    Route::any('blog/:id','blog/read',[],['id'=>'\d+']);
    Route::any('blog/:name','blog/read',[],['name'=>'\w+']);
  }
});

Third, the route is bound

1, bound to the module / controller / operator (bind up to operation level)

// current URL binding module to index 
the Route :: the bind ( 'index'); 
// bind to the current URL index controller module's blog 
the Route :: the bind ( 'index / blog'); 
// bind current URL to read the module index blog operation of the controller 
Route :: bind ( 'index / blog / read');

Personal: So far no difference route is bound to effect registration using dynamic routing, but there is no need to define routing expressions, namely simple and quick

http://serverName/index/blog/read/id/5

http://serverName/read/id/5

Personal: but the route is bound to be + registered routing modules, controllers, methods, parameters passed all the hidden name, but only registered route, then only hide modules, controllers, transmission parameter name, there is a routing expression

Route::get('index/blog/:id','index/blog/read');

http://serverName/5

2, bound to a namespace

Personal: This is a very easy way binding, to do the project when the original model and controller are what need to be namespace declaration at the top, while using namespace binding, a module requires only two, four two modules, Would not it be super global variable? But just from the URL is omitted only the modules of this layer.

// bind to the namespace 
Route :: bind ( '\ app \ index \ controller', 'namespace');

Just

http://serverName/blog/read/id/5

It can directly access the \ app \ index \ read method controller \ Blog category.

3, bound to class

Individual: Compared namespace, where a single module is omitted, the controller for the URL from the two layers. Similarly namespace, where a single layer for this controller, and a module for the namespace layer

// bind to class 
Route :: bind ( '\ app \ index \ controller \ Blog', 'class');

Just

http://serverName/read/id/5

It can directly access the \ app \ index \ read method controller \ Blog category.

Note: After binding to the namespace and class, not a module initialization.

4, entry file bindings

Personal: If we use the import documents binding, bind a module, and the module cancel multi-access configuration ↓, it means that even if you put more items in the project will only access one. You want to access other modules only need to change the name at the entrance to the file. You can even use an empty module. Bind entry file is empty module will implement the project offline functionality.

// whether to support multi-module 
'app_multi_module' => true,

Only needs to add an entry file BIND_MODULE constants, binding to the specified file to the current inlet module or the controller, for example:

If we need to file an entrance binding module, you can use the following two ways:

① constant defines

// define application directory 
DEFINE ( 'APP_PATH', __DIR__ '/../application/'.); 
// bind to the index module 
DEFINE ( 'BIND_MODULE', 'index'); 
// loading frame guide files 
require __DIR__. '/../thinkphp/start.php';

② automatic entrance binding

Individual: As the name suggests, ① and function the same, but this need not even binding. Only you need to open the configuration can be ↓

// inlet autobinding module 
'auto_bind_module' => false,

Fourth, routing domain

// blog subdomains bind to the blog module 
Route :: domain ( 'blog', 'blog '); 
// blog subdomains bind to blog module, and insert a default parameter 
Route :: domain ( 'blog', 'blog ? var = thinkphp ');

// original URL access 
http://www.thinkphp.cn/blog/article/read/id/5 
// bind to the blog subdomain access 
http://blog.thinkphp.cn/article/read/id/ 5
// index blog subdomains bind to the controller module's blog 
Route :: domain ( 'blog', 'index / blog');

// original URL access 
http://www.thinkphp.cn/index/blog/read/id/5 
// bind to the blog subdomain access 
http://blog.thinkphp.cn/read/id/5

// full domain admin module bound to 
Route :: Domain ( 'admin.thinkphp.cn', 'admin'); 
// IP bound to the admin module 
Route :: domain ( '114.23.4.5', 'admin') ;

Pan-domain deployment

Two pan-domain

// Bind the Pan-secondary domain name to the book block 
Route :: domain ( '*', 'book name = *?');

And directly through $_GET['name']to get the current pan-domain variables.

The following URL access will be direct access to the book module

http://hello.thinkphp.cn
http://quickstart.thinkphp.cn

Three pan-domain

// Bind the Pan-level domain to the user module 
Route :: domain ( '* user. ', 'User name = *?');

If we go to the following URL address:

http://hello.user.thinkphp.cn
the same time, in addition to access user module, the will to default incoming $_GET['name'] = 'hello'

In the configuration parameters that you pass the time, if you need to use the current pan-domain name as a parameter can be set directly as "*" to

Guess you like

Origin www.cnblogs.com/68xi/p/11528013.html