PHP U method is used to complete the assembly of URL addresses. Its characteristic is that it can automatically generate the corresponding URL address based on the current URL mode and settings. Its syntax format is "U('address','parameter', 'Pseudo-static', 'Whether to jump', 'Display domain name');".
Detailed explanation of ThinkPHP function: U method
The U method is used to complete the assembly of the URL address. The feature is that it can automatically generate the corresponding URL address based on the current URL mode and settings. The format is:
U('address','parameter','pseudo-static','whether to jump','display domain name ');
The advantage of using the U method in the template instead of fixing the hard-coded URL address is that once your environment changes or the parameter settings change, you do not need to change any code in the template.
The calling format in the template needs to be {:U('address', 'parameter'...)}
Recommended tutorial: "thinkphp framework》
Usage example of U method:
U('User/add') // 生成User模块的add操作地址
Can also support group calls:
U('Home/User/add') // 生成Home分组的User模块的add操作地址
Of course, it can also be just Write the operation name, which means calling the current module
U('add') // 生成当前访问模块的add操作地址
In addition to the group, module and operation name, we can also pass in some parameters:
U('Blog/read?id=1') // 生成Blog模块的read操作 并且id为1的URL地址
The second parameter of the U method supports passing Input parameters, support two definition methods: array and string. If only string parameters can be defined in the first parameter, the following methods 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')
But they are not allowed to be used. The following definition method is used to pass parameters:
U('Blog/cate/cate_id/1/status/1')
According to different URL settings of the project, the same U method call can intelligently produce different URL address effects, for example, for this definition:
U('Blog/read?id=1')
For 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 generated URL address is:
http://serverName/index.php/Blog/read/id/1
If the current URL is set to REWRITE mode, the URL address finally generated by the same method is:
http://serverName/Blog/read/id/1
If you also set the PATHINFO delimiter:
'URL_PATHINFO_DEPR'=>'_'
will be generated
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 URL address finally generated by the same method is:
http://serverName/Blog/read/id/1.html
If multiple pseudo-static supports are set, Then the first pseudo-static suffix will be automatically added to the end of the URL address. Of course, 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
U method can also support routing. If we define a routing rule as:
'news/:id\d'=>'News/read'
Then we can use
U('/news/1')
The final generated URL address is:
http://serverName/index.php/news/1
If your application involves the operation address of multiple subdomain names, you can also specify the domain name that needs to generate the address in the U method, for example:
U('Blog/read@blog.thinkphp.cn','id=1');
@Just pass in the domain name that needs to be specified later.
In addition, if the fifth parameter of the U method is set to true, it means that the current domain name is automatically recognized, and the subdomain name of the current address is automatically generated based on the subdomain name deployment settings APP_SUB_DOMAIN_DEPLOY and APP_SUB_DOMAIN_RULES.
If URL_CASE_INSENSITIVE is turned on, lowercase URL addresses will be generated uniformly.
Starting from version 3.1.2, U method can also support generating anchor points in URL addresses, 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 are used at the same time, please note that the order is anchor first and then the domain name, for example:
U('Blog/read#comment@blog','id=1');
via:http://www.thinkphp.cn/document/132.html
The above is the detailed content of What is the use of PHP U method?. For more information, please follow other related articles on the PHP Chinese website!