thinkphp URL rules, url pseudo-static, url routing, url rewriting, url generation (15)

Source: Internet
Author: User

Original: thinkphp URL rules, url pseudo-static, url routing, url rewriting, url generation (15)

This section: Details thinkphp URL rules, url pseudo-static, url routing, url rewriting, url generation



one, URL rules
1, default is case-sensitive  ,
2, if we do not want to be case-sensitive can be changed to config file
Span style= "White-space:pre" > ' url_case_insensitive ' =>true,//url case-insensitive
* Module name is too long:
A, if the module is named Usergroupaction, complex modules (typically Indexaction)
then the URL-looking module is necessary to write
http://localhost/thinkphp4/index.php/user_group/index
B, if ' url_case_insensitive ' =>false (case-sensitive condition can be accessed)
then the URL can also be written as
http://localhost/ Thinkphp4/index.php/usergroup/index


Second, URL pseudo-static (TP default support pseudo-static)
Http://localhost/thinkphp4/index.php/UserGroup/index.xml
* Default PDO, HTML, XML ... is supported, if you want to limit the configuration.
' Url_html_suffix ' = ' html|shtml|xml ',//limit pseudo-static suffix


third, URL routing
1. Start the route
To turn on routing support in the configuration file
' url_router_on ' = = true,//Turn on routing
2. Using routing
1. Rule Expressions Configure Routing
' url_router_on ' = = true,//Turn on routing
' Url_route_rules ' = Array (
' My ' = ' index/index ',//static address routing access: Http://localhost/thinkphp/index.php/my
': Id/:num ' + ' index/index ', */* After the numeric value of random write, the letter will be OK
Dynamic Address Routing access: http://localhost/thinkphp/index.php/10/200
You can pass a value or get it in the module controller using get
echo $_get[' id '];
echo $_get[' num '];
*/
' My/:num ' = ' index/index ',//mixed address routing http://localhost/thinkphp/index.php/my/200
' year/:year/:month/:d ate ' = ' index/index ',//dynamic and static mixed address routing: Http://localhost/thinkphp/index.php/ye Ar/2014/12/21
' year/:year\d/:month\d/:d ate\d ' = ' index/index ',//dynamic and static mixed address routing--plus \d representative type can only be numeric
' my/:id$ ' = ' index/index ',//plus $ Description The address can only be my/1000 there's nothing else left behind.
);


2. Regular expression Configuration routing
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. Precautions:
1. The more complex the route the more forward
' Url_route_rules ' =>array (
' my/:year/:month:/:d ay ' = ' index/day ', * Complex routes are placed in front of them and will not be executed at the back
' my/:id\d ' = ' index/index ',
' My/:name ' = ' index/index ',
)
2. You can use the route rule as an exact match (no matter the complex no, it will match all the regular)
' Url_route_rules ' =>array (
' my/:id\d$ ' = ' index/index ',
' my/:name$ ' = ' index/index ',
' my/:year/:month:/:d ay$ ' = ' index/day ',
),
3. Using regular matching methods
' 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 ',
),


iv. URL Rewriting
For example: http://localhost/thinkphp/index.php/Index/index.html/t/my----don't want index.php to show up.
The following is the Apache configuration process, you can refer to the following:
1. The mod_rewrite.so module is loaded in the httpd.conf configuration file
2. AllowOverride None Change None to all
3. Make sure Url_model is set to 2 (this step is omitted)
4. Save the following content as a. htaccess file in a sibling directory of the portal file
<ifmodule mod_rewrite.c>
Rewriteengine on
Rewritecond%{request_filename}!-d
Rewritecond%{request_filename}!-f
Rewriterule ^ (. *) $ index.php/$1 [qsa,pt,l]
</IfModule>
After restarting Apache, the original
You can access it by accessing
http://localhost/thinkphp/Index/index.html/t/my-simplified URL address, SEO support is better


v. URL generation(The manual is described in detail)
Public Function URL () {
echo U (' Index/add '); The URL address of the add operation that generated the index module
Thinkphp/index.php/index/add
}


thinkphp URL rules, url pseudo-static, url routing, url rewriting, url generation (15)

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.