thinkphp routing rules

Regular route is a route definition easier to understand way, using regular expressions ThinkPHP designed to define.

Regular expressions

Regular expressions usually contain static and dynamic addresses, or a combination of the two addresses, for example, the following are all valid regular expression:

  1. 'my' => 'Member/myinfo', // 静态地址路由
  2. 'blog/:id' => 'Blog/read', // 静态地址和动态地址结合
  3. 'new/:year/:month/:day'=>'News/read', // 静态地址和动态地址结合
  4. ':user/:blog_id' =>'Blog/read',// 全动态地址

The definition of regular expressions with the "/" as the delimiter argument, without URL_PATHINFO_DEPRaffecting settings

Each parameter to ":" parameter indicates the beginning are dynamic parameters, and will automatically correspond to a GET parameter, for example, :idwhich means the parameter can be used to match the $_GET['id']way to obtain, :year:month , :day respectively corresponding to $_GET['year']$_GET['month'] and  $_GET['day'].

Digital constraints

Detecting the type of support for variables, but only support numeric constraint definition, e.g.

  1. 'blog/:id\d'=>'Blog/read',

It represents only matches digital parameters, if you need more and more variable type detection, use regular expressions to define to resolve.

Length constraint is not currently supported, if necessary using regular defined solve

Support functions

It can support function to filter routing variables, such as:

  1. 'blog/:id\d|md5'=>'Blog/read',

He told to match the id md5 process variables, that is, the actual method of operation of the incoming read $_GET['id'] actually  md5($_GET['id']).

Note: do not support the use of multiple processing functions and additional parameters passed to the function variables.

Optional definitions

Support for optional definition of routing parameters, such as:

  1. 'blog/:year\d/[:month\d]'=>'Blog/archive',

[:month\d]Variables [] indicates that the variable contains up after matching optional routing variable.

After the above-defined routing rules, access the following URL address can be matched correctly route:

  1. http://serverName/index.php/Home/blog/2013
  2. http://serverName/index.php/Home/blog/2013/12

With the optional variable is defined, where before the need to define two or more routing rules to be processed may be combined into a routing rule.

Optional parameters can only be placed routing rules Finally, if you use the optional parameter in the middle, then the back of the variables will become optional parameters.

Rules out

Non-numeric variable support simple exclusion, mainly played the role of resolve to avoid confusion, for example:

  1. 'news/:cate^add|edit|delete'=>'News/category'

3.2.2 release, functions and rules in order to avoid conflict, the routing rule excluded separator to "-", so the above route definition needs to be changed: 'news /: cate ^ add-edit-delete' => 'News / category '

Because of the limitations defined by the rules, as it happens inside our routing rules news and actual news module is the same name, and :catedoes not automatically distinguish between dynamic parameters of the current URL which is the actual name of the operation or routing variables, so in order to avoid confusion, we need to do some routing variables cate exclusion to help us more accurately match routing, format ^add|edit|delete , said in addition to match all strings add edit and delete the outside, we recommend a better way or improve your routing rules, avoid routing rules and modules with the same name exist, e.g.

  1. 'new/:cate'=>'News/category'

We can more easily define the routing rules.

Exact match

When matching rule to detect only the match from the beginning of the URL, as long as the URL contains the definition of routing rules will match the success, if you want an exact match, you can use the $ symbol, for example:

  1. 'new/:cate$'=> 'News/category'

http://serverName/index.php/Home/new/info

It will match success,

http://serverName/index.php/Home/new/info/2

It will not match the success. ,

Marble platform accuracy class

If it is adopted

  1. 'new/:cate'=> 'News/category'

Define words, the URL can be accessed two ways of successfully matched.

Routing rules exact match if you use the optional parameters, then it will become invalid.

 

Guess you like

Origin www.cnblogs.com/furuihua/p/11776162.html