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 calling format in the template requires the use of the
{: U (' address ', ' Parameters ' ...)}The way
Examples of usage of the Basic usage U method:
- U(' User/add ') //Generate Add Action address for User module
You can also support grouping calls:
- U(' Home/user/add ') //Generate the Add action address of the User module for the Home group
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
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
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 ')
However, it is not allowed to use the following definition to pass parameters:
- U(' blog/cate/cate_id/1/status/1 ')
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 ')
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
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
If the current URL is set to rewrite mode, the last URL generated by the same method is:
- HTTP://servername/blog/read/id/1
If you also set the PathInfo delimiter:
- ' Url_pathinfo_depr '= '_ '
Will generate
- HTTP://servername/blog_read_id_1
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
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 ')
Will generate
- HTTP://servername/blog/read/id/1.xml
The route support U method can also support routing if we define a routing rule for:
- ' news/:id\d '= 'news/read '
Then you can use
- U('/news/1 ')
The resulting URL address is:
- HTTP://servername/index.php/news/1
Domain support If your app involves more than one subdomain's operating address, you can also specify the domain name in the U method that needs to generate the address, for example:
- U(' blog/[email protected] ',' id=1 ');
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. The anchor supports the 3.1.2 version, and the U method can also support the generation of anchor points in the URL address, for example:
- U(' blog/read#comment ',' id=1 ',' HTML ')
Will generate
- HTTP://servername/blog/read/id/1.html#comment
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