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 invocation format in the template requires the use of the {: U (' address ', ' Parameters ' ...)} approach to the basic usage example of the U method:
- U(‘User/add‘) // 生成User模块的add操作地址
复制代码
You can also support grouping calls:
- U(‘Home/User/add‘) // 生成Home分组的User模块的add操作地址
复制代码
Of course, it can be just a write operation name, which means that the current module is called
- U(‘add‘) // 生成当前访问模块的add操作地址
复制代码
In addition to grouping, modules, and operation names, 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 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