ThinkPHP Route details

Source: Internet
Author: User
ThinkPHP is a url transmission rule, for example, index. php? The gHome & amp; mIndex & amp; aprice rule can also be changed. & amp; you can set it to @ or another rule, such as pathinfo, we also use the basic configuration when setting pseudo-static, so that we can access the default homepage of our application. To access the project directory, you can directly use the PHP built-in server to start access. for example:

php -S localhost:8999

Enter localhost: 8999 in the browser to view the default homepage of ThinkPHP: a smiling face.

Here, we access index. php, the default entry file that comes with ThinkPHP, that is, the index () method of IndexController. this is because ThinkPHP default settings:

'DEFAULT_CONTROLLER'  => 'Index'

If you have checked the ThinkPHP/Conf/convention. php file, you will understand that this is actually the default controller.

We will give a closer look at the Controller.

After learning about this basic knowledge, what if we need to access other pages and access other controllers and methods? The answer is in the routing tutorial in this section.

Routing rules

Before using a route, make sure that your URL supports the PATH_INFO (or compatible with the URL mode, but does not support the routing function when using the normal URL mode) and make sure that the following route settings are enabled:

'URL_ROUTER_ON'  => true

Two configuration items, PATH_INFO and URL_ROUTER_ON, can be found in the ThinkPHP/Conf/convention. php file.

After the preceding conditions are met, you can configure routing rules. Use the URL_ROUTE_RULES parameter in the configuration file for configuration. the configuration format is an array in the following format: 'route expression' => 'Route address and input parameter' each element represents a routing rule, for example:

'URL_ROUTE_RULES'=>array(  'blogs/:year/:month/:day' => array('Index/archive', 'status=1'),  'blogs/:id'        => 'Index/read',),

ThinkPHP matches the routing rules in the defined order in sequence. Once the rules are matched, the controllers and operation methods in the routing definition are located for execution (you can input other parameters ), the subsequent rules do not continue to match.

The preceding route configuration description: in each route expression, the parameter name is followed by the parameter name. for example, the preceding parameters year, month, and id are parameter names, it points to the read method of the Index controller. this method accepts a $ id parameter:

public function read($id){    echo "read page with" .$id;  }

Enter http: // localhost: 8999/index. php/Home/blogs/2 in the browser.

read page with 2

Home indicates the Home module. you can map it to the corresponding Home directory.

'DEFAULT_MODULE'    => 'Home'

You can modify the settings as needed, but the default Home module is still used in this course.

If you still need to pass additional parameters, like the status in the first rule array ('index/archive', 'status = 1, you can see that multiple such parameters are set.

If you try to enter:

Http: /localhost: 8999/index. php/Home/blogs/string

ThinkPHP also returns a string, but in daily development, we usually need to limit that the id variable is an integer. what should we do? You only need to make a few changes.

'blogs/:id\d'        => 'Index/read',

The above \ d indicates that the variable id can only be a number.

Optional parameters can be expressed in [], for example:

'blogs/:year/:month/[:day]' => array('Index/archive', 'status=1'),

The day above is now an optional parameter. you can pass it to another user or not.

In ThinkPHP, you can also restrict the suffix of a route and use a regular route.

Restrict the route suffix. Generally, the common suffixes such as html and htm are used, or the above rules are used as an example:

'blogs/:id'        => array('Index/read',array('ext'=>'html'))

You can restrict this rule only when the routing suffix of .html takes effect.

Regular routing

Regular expressions are a lot of learning. before learning ThinkPHP's regular routing, it is best to have a certain regular expression basis.

The regular expressions supported by the routing expression must start with "/". Otherwise, it is regarded as a rule expression. for example:

'#^blog\/(\d+)$#' => 'Index/read'

This will be resolved as a rule route rather than a regular route, because the recording expression does not start with/, so we need to write as follows:

'/^ New \/(\ d {4}) \/(\ d {2}) $/' => 'index/achive? Year =: 1 & month =: 2 ',
The above is a correct regular route. For the sub-pattern of each regular expression rule (such as d {4} and d {2}), you can use: 1 ,: 2. in this way, the serial number is the serial number of the submode.

Static routing

The ThinkPHP framework also has a routing mechanism called static routing, which is actually a static simplified version of rule routing. the routing definition does not contain dynamic parameters (the id parameter in the preceding routing rules ), static routes do not need to traverse routing rules but are directly located. Therefore, the execution efficiency is high. Static routing uses URL_MAP_RULES to define rules:

'URL_ROUTER_ON'  => true,'URL_MAP_RULES'=>array(  'new/top' => 'Index/top?type=top')

Due to Index/top? In type = top, Index indicates the controller, and the first top indicates the method. Therefore, we need to create the top method in the Index controller:

public function top(){    echo "top page 
"; }

According to the above rule, if we access

Http: // localhost: 8999/index. php/Home/new/top

In fact, we access:

Http: // localhost: 8999/index. php/Home/index/top/type/top
The new/top method corresponds to the top method of the index controller. the parameter of the transmitter is type and the parameter value is top. Therefore, index/top/type/top is available.

However, when we access http: // localhost: 8999/index. php/Home/new/top/var/test although there is a new/top in front of the URL address, but because static routing is completely matched, so it does not match index/top/type/top

The above is all the content of this article. I hope you will like it.

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.