What is the use of PHP U method?

藏色散人
Release: 2023-03-01 21:28:01
Original
2629 people have browsed it

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');".

What is the use of PHP U method?

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

Basic usage

Usage example of U method:

U('User/add') // 生成User模块的add操作地址
Copy after login

Can also support group calls:

U('Home/User/add') // 生成Home分组的User模块的add操作地址
Copy after login

Of course, it can also be just Write the operation name, which means calling the current module

U('add') // 生成当前访问模块的add操作地址
Copy after login

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地址
Copy after login

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')
Copy after login

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')
Copy after login

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')
Copy after login

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
Copy after login

If the current URL is set to PATHINFO mode, the last generated URL address is:

http://serverName/index.php/Blog/read/id/1
Copy after login

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
Copy after login

If you also set the PATHINFO delimiter:

'URL_PATHINFO_DEPR'=>'_'
Copy after login

will be generated

http://serverName/Blog_read_id_1
Copy after login

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
Copy after login

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')
Copy after login

will generate

http://serverName/Blog/read/id/1.xml
Copy after login

Routing support

U method can also support routing. If we define a routing rule as:

'news/:id\d'=>'News/read'
Copy after login

Then we can use

U('/news/1')
Copy after login

The final generated URL address is:

http://serverName/index.php/news/1
Copy after login

Domain name support

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');
Copy after login

@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.

Anchor support

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')
Copy after login

will generate

http://serverName/Blog/read/id/1.html#comment
Copy after login

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');
Copy after login

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!

Related labels:
php u
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!