Thinkphp Learning Note 10-a routing rule that is not understood

Source: Internet
Author: User

Original: thinkphp Study note 10-a routing rule that is not understood

This part of the route seems to be in the actual work not how to design, just in the default settings, in the manual to see part, difficult to understand.

1. Route definition

What does it take to support path_info,path_info to use the routing feature? The manual mentions "to use the routing feature, provided that your URL supports path_info (or a compatible URL pattern, which does not support routing in normal URL mode)," and that the URL supports path_info, not Apache to support Path_info, Degree Niang Talk is also clear, see below:

PathInfo
(PHP 4 >= 4.0.3, PHP 5)
PathInfo--Returns information about the file path
Description
Array pathinfo (string path [, int options])
PathInfo () returns an associative array containing information about path. Includes the following array cells: dirname,basename and extension.
You can specify which cells to return through the parameters options. They include: Pathinfo_dirname,pathinfo_basename and Pathinfo_extension. The default is to return all cells.
Example 1. PathInfo () example
<?php
$path _parts = PathInfo ("/www/htdocs/index.html");
echo $path _parts["DirName"]. "\ n";
echo $path _parts["basename"]. "\ n";
echo $path _parts["extension"]. "\ n";
?>
The example above will output:
/www/htdocs
Index.html
Html

When I wrote this, I didn't output anything on my machine, and later I found php.ini in the PHP installation directory to open the cgi.fix_pathinfo=1, and then I was able to output the content.

But this is just about the use of a function, which is pathinfo this function returns an array of three elements that are

DirName: I understand that the server name plus the path,
BaseName: The name of the file accessed,
Extension: File name extension

After the server is supported, you also need to open the appropriate configuration in the configuration file.

// turn on Route ' url_router_on '   true

This configuration is also defined in the thinkphp\conf\convention.php file, but it can be overridden in the module's configuration file. Then is the definition of routing rules, the matching of the route is based on a first-come first-served order, after the first successful match will not be matched again, but access to the controller and methods in the route, and pass in parameters, to get the results.

The format of a routing rule definition is:' route expression ' = ' + ' routing address and incoming parameter ' or array (' route expression ', ' Routing address ', ' incoming parameters ')

The route expression contains the rule route Ha Zheng the route

Expression examples
Regular Expression/^blog\/(\d+) $/
Rule expression Blog/:id

The routing address represents the address to which the current route expression needs to be routed, contains internal addresses and external addresses, and allows implicitly passed parameters that are not in the URL, allowing the use of strings or arrays to define the routing function, which can be defined in the following 6 ways, by using a closure function.

How to define Defining Formats
Mode 1: route to internal address (string) ' [Group/module/operation]? extra parameter 1= value 1& extra parameter 2= value 2 ... '
Method 2: Route to internal address (array) parameters in string mode Array (' [Group/module/operation] ', ' extra parameter 1= value 1& extra parameter 2= value 2 ... ')
Mode 3: route to internal address (array) parameter in array mode Array (' [Group/module/operation] ', array (' extra parameter 1 ' = ' = ' value 1 ', ' extra parameter 2 ' = ' = ' value 2 ' ...) [, routing parameters])
Mode 4: route to external Address (string) 301 redirect ' External address '
Mode 5: route to an external address (array) to specify the redirect code Array (' External address ', ' redirect code ' [, route parameter])
Mode 6: Closure function function ($name) {echo ' Hello, '. $name;}

I don't understand it at all! Internal address, external address? What is the concept of a local area network address? 192.168.0.1 like that? University of all forget, Baidu a bit, it seems like this, there are three types of internal address, used to connect the LAN devices, including printers, etc.?

Routes that start with HTTP or/are considered to be an external address or redirect address, such as

' Blog/:id ' + '/blog/read/id/:1 ' uses 301 redirect way to Route jumps, this way the advantage is that the URL can be more arbitrary, including in the URL to pass more non-standard format parameters, wipe completely do not understand, non-standard format parameters for what? Too TM Professional.

' Blog/:id ' = ' blog/read ': only modules and operating addresses are supported, for example, we want toavatar/123重定向到/member/avatar/id/123_small只能使用‘avatar/:id‘=>‘/member/avatar/id/:1_small‘ 貌似:id代表一个参数,参数名字是id。路由地址采用重定向地址的话,如果要引用动态变量,也是采用 :1、:2 的方式。采用重定向到外部地址通常对网站改版后的URL迁移过程非常有用,例如:‘blog/:id‘=>‘http://blog.thinkphp.cn/read/:1‘ 表示当前网站(可能是http://thinkphp.cn)的 blog/123 地址会直接重定向到 http://blog.thinkphp.cn/read/123

2. Rule Routing

The rule expression contains either a static address and a dynamic address, or a combination of two addresses, as follows:

"My" = "member/myinfo",//Static routing address
"Blog/:id" = "blog/read",//static address and dynamic address combination,
"new/:year/:month/:d ay" = "news/read",//combination of static address and dynamic address
": user/:blog_id" = "blog/read",//Full dynamic address

Arguments with ":" Start with the dynamic parameters, automatically corresponding to a get parameter, such as ": id" means here can use $_get[' ID ') to obtain parameters, and ": Year", ": Month", ":d ay" corresponding to $_get[' year '],$_get[ ' Month '],$_get[' Day ' to get.

number constraints : Support for variable type detection, currently only supports the definition of a constraint on a numeric type, such as ' blog/:id\d ' + ' blog/read ', which means matching only numeric parameters,
function Support: can support the function filtering of the routing variable, for example ' blog/:id\d|md5 ' = ' blog/read ', for MD5 processing of the matched variable, the $_get[' id ' of the actual incoming read operation method, In fact, MD5 ($_get[' ID '), it is not supported to use multiple function handling and function extra parameter handling on variables.

  

Thinkphp Learning Note 10-a routing rule that is not understood

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.