ThinkphpURL rules, URL pseudo-static, URL routing, URL rewriting, URL generation (15th) _ PHP Tutorial

Source: Internet
Author: User
ThinkphpURL rules, URL pseudo-static, URL routing, URL rewriting, URL generation (15th ). ThinkphpURL rules, URL pseudo-static, URL routing, URL rewriting, URL generation (15th) this chapter: details about thinkphpURL rules, URL pseudo-static, URL routing, URL rewriting, URL generation I. thinkphp URL rules, URL pseudo-static, URL routing, URL rewriting, and URL generation (15th)

This section describes thinkphp URL rules, URL pseudo-static, URL routing, URL rewriting, and URL generation in detail.

I. URL rules
1. the default value is case sensitive.
2. you can change the configuration file if you do not want to be case sensitive.
'URL _ CASE_INSENSITIVE '=> true, // The URL is case insensitive.
* When the module name is too long:
A. If the module name is UserGroupAction, the complicated module (usually IndexAction)
Then, you need to write the url module
Http: // localhost/thinkphp4/index. php/user_group/index
B. If 'URL _ CASE_INSENSITIVE '=> false (case-sensitive access is allowed)
The url can also be written
Http: // localhost/thinkphp4/index. php/UserGroup/index


II. URL pseudo-static(Tp supports pseudo-static by default)
Http: // localhost/thinkphp4/index. php/UserGroup/index. xml
* By default, pdo, html, and xml... are all supported. if you want to restrict the addition of configuration
'URL _ HTML_SUFFIX '=> 'HTML | shtml | XML', // restrict pseudo-static suffixes


III. URL routing
1. start the route
To enable route support in the configuration file
'URL _ ROUTER_ON '=> true, // enable routing
2. use routing
1. configure routes using rule expressions
'URL _ ROUTER_ON '=> true, // enable routing
'URL _ ROUTE_RULES '=> array (
'My => 'index/Index', // static address route access: http: // localhost/thinkphp/Index. php/my
': Id/: num' => 'index/Index', and the numeric value after/* can be written with any letter
Dynamic address routing access: http: // localhost/thinkphp/index. php/10/200
You can use the get method to pass values or obtain values in the module controller.
Echo $ _ GET ['id'];
Echo $ _ GET ['num'];
*/
'My/: num' => 'index/Index', // dynamic and static hybrid address routing http: // localhost/thinkphp/Index. php/my/200
'Year/: year/: month/: date' => 'index/Index', // dynamic and static hybrid address routing: http: // localhost/thinkphp/Index. php/year/2014/12/21
'Year/: year \ d/: month \ d/: date \ d' => 'index/Index ', // dynamic and static hybrid address routing -- add \ d to indicate that the type can only be numbers
'My/: id $ '=> 'index/Index', // add $ to indicate that the address can only be my/1000 followed by no other content
);


2. configure routes using regular expressions
// Http: // localhost/thinkphp/index. php/year/2014/12/21
'/^ Year \/(\ d {4}) \/(\ d {2}) \/(\ d {2})/' => 'index/Index? Year =: 1 & month =: 2 & date =: 3'


3. notes:
1. the more complex the route is, the more advanced it is.
'URL _ ROUTE_RULES '=> array (
'My/: year/: month:/: day' => 'index/Day'. * complex routes are placed in front of them, and will not be executed after them.
'My/: id \ d' => 'index/Index ',
'My/: name' => 'index/Index ',
)
2. you can use $ as a completely matched routing rule (no matter the complexity or not, all the regular expressions will be matched)
'URL _ ROUTE_RULES '=> array (
'My/: id \ d $ '=> 'index/Index ',
'My/: name $ '=> 'index/Index ',
'My/: year/: month: //: day $ '=> 'index/Day ',
),
3. Regular Expression Matching
'URL _ ROUTE_RULES '=> array (
'/^ My \/(\ d +) $/' => 'Index/index? Id =: 1 ',
'/^ My \/(\ w +) $/' => 'Index/index? Name =: 1 ',
'/^ My \/(\ d {4}) \/(\ d {2}) \/(\ d {2 }) $/'=> 'index/day? Year =: 1 & month =: 2 & day =: 3 ',
),


4. URL rewriting
For example, http: // localhost/thinkphp/index. php/Index/index.html/t/my ---- doesn't want index. php to appear
The configuration process of Apache is as follows:
1. the mod_rewrite.so module is loaded in the httpd. conf configuration file.
2. change AllowOverride None to All
3. make sure that URL_MODEL is set to 2 (skip this step)
4. Save the following content as a. htaccess file and put it under the same directory of the entry file

RewriteEngine on
RewriteCond % {REQUEST_FILENAME }! -D
RewriteCond % {REQUEST_FILENAME }! -F
RewriteRule ^ (. *) $ index. php/$1 [QSA, PT, L]

After Apache is restarted, the original
You can access
Http: // localhost/thinkphp/Index/index.html/t/my -- simplifies the URL address and improves SEO support.


5. URL generation(The manual is described in detail)
Public function url (){
Echo U ('index/add'); // Generate the URL of the add operation of the Index module
/// Thinkphp/index. php/index/add
}

Previous http://qdxinbj8.2cto.com/index.php? M = content & c = content & a = public_preview & steps = 1 & catid = 75 & id = 363637

Http://www.bkjia.com/PHPjc/934466.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/934466.htmlTechArticlethinkphp URL rules, URL pseudo static, URL routing, URL rewriting, URL generation (15) This section: thinkphp URL rules, URL pseudo-static, URL routing, URL rewriting, URL generation 1 ,...

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.