The U method is used to complete the assembly of URL addresses, which is characterized by the ability to automatically generate corresponding URL addresses based on the current URL pattern and settings, in the following format:
U (' address ', ' parameters ', ' pseudo-static ', ' whether Jump ', ' Show domain name ');
The advantage of using the U method instead of a fixed write dead URL in a template is that once your environment changes or the parameter settings change, you don't need to change any of the code in the template.
The call format in the template needs to take the form {: U (' address ', ' Parameters ' ...)}
Basic usage
Example of the use of the U method:
- U(' User/add ') //Generate Add Action address for User module
Copy Code
You can also support grouping calls:
- U(' Home/user/add ') //Generate the Add action address of the User module for the Home group
Copy Code
Of course, it can be just a write operation name, which means that the current module is called
- U(' Add ') //Generate Add action address for current access module
Copy Code
In addition to grouping, modules, and operation names, we can also pass in some parameters:
- U(' blog/read?id=1 ') //Generate a read operation for the Blog module and a URL address with ID 1
Copy Code
The second parameter of the U method supports passing arguments, supports both array and string definitions, and if only a string-only parameter can be defined in the first argument, the following are equivalent:
- U(' blog/cate ',array(' cate_id '=1,' status '=1))
- U(' blog/cate ',' Cate_id=1&status=1 ')
- U(' Blog/cate?cate_id=1&status=1 ')
Copy Code
However, it is not allowed to use the following definition to pass parameters:
- U(' blog/cate/cate_id/1/status/1 ')
Copy Code
Depending on the URL settings of the project, the same U method call can intelligently correspond to different URL address effects, for example:
- U(' blog/read?id=1 ')
Copy Code
This definition is an example.
If the current URL is set to normal mode, the last generated URL address is:
- HTTP://servername/index.php?m=blog&a=read&id=1
Copy Code
If the current URL is set to PathInfo mode, the last URL generated by the same method is:
- HTTP://servername/index.php/blog/read/id/1
Copy Code
If the current URL is set to rewrite mode, the last URL generated by the same method is:
- HTTP://servername/blog/read/id/1
Copy Code
If you also set the PathInfo delimiter:
- ' Url_pathinfo_depr '= '_ '
Copy Code
Will generate
- HTTP://servername/blog_read_id_1
Copy Code
If the current URL is set to rewrite mode, and the pseudo-static suffix is set to HTML, the last URL generated by the same method is:
- HTTP://servername/blog/read/id/1.html
Copy Code
If more than one pseudo-static support is set, the first pseudo-static suffix is automatically appended to the URL address, but you can also manually specify the pseudo-static suffix to be generated in the U method, for example:
- U(' Blog/read ',' id=1 ',' xml ')
Copy Code
Will generate
- HTTP://servername/blog/read/id/1.xml
Replication Code Routing Support
The U method can also support routing if we define a routing rule as:
- ' news/:id\d '= 'news/read '
Copy Code
Then you can use
- U('/news/1 ')
Copy Code
The resulting URL address is:
- HTTP://servername/index.php/news/1
Copy Code domain support
If your application involves multiple subdomains, you can also specify the domain name in the U method that you want to generate the address for, for example:
- U(' blog/[email protected] ',' id=1 ');
Copy Code
After the @ is passed in to the domain name you want to specify.
In addition, the 5th parameter of the U method, if set to true, indicates that the current domain name is automatically recognized and automatically matches the subdomain that generated the current address according to the subdomain deployment settings App_sub_domain_deploy and App_sub_domain_rules automatically.
If Url_case_insensitive is turned on, a lowercase URL address is generated uniformly.
Anchor Point Support
Starting with the 3.1.2 version, the U method can also support the generation of anchor points in the URL address, for example:
- U(' blog/read#comment ',' id=1 ',' HTML ')
Copy Code
Will generate
- HTTP://servername/blog/read/id/1.html#comment
Copy Code
If the domain name and anchor point are used at the same time, note that the order is the first anchor point after the domain name, for example:
- U(' blog/read#[email protected] ',' id=1 ');
thinkphp function: U method