ThinkPHP Route details

Source: Internet
Author: User
ThinkPHP is a url transmission rule, for example, index. php? The gHomemIndexaprice rule can also be changed. You can set it to @ or another rule, such as pathinfo,

ThinkPHP is a url transmission rule, for example, index. php? G = Homem = Indexa = price this rule can also be changed. You can set it to @ or another rule, such as pathinfo,

With the basic configuration, 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 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:

: 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 ('text' => '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 (for example, \ d {4} And \ d {2}), you can use: 1. 2. 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

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

In fact, we access:

: 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: 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.