Home > PHP Framework > ThinkPHP > body text

How to use thinkphp u method

藏色散人
Release: 2021-12-16 14:58:44
Original
2350 people have browsed it

thinkphp's u method is used to complete the assembly of URL addresses. Its feature is that it can automatically generate the corresponding URL address based on the current URL mode and settings. Its usage syntax is "U('address','parameter' ,'pseudo-static','whether to jump','display domain name');".

How to use thinkphp u method

The operating environment of this article: Windows 7 system, ThinkPHP version 5.0, Dell G3 computer.

How to use the u method of thinkphp?

U method is used to complete the assembly of the URL address. Its feature is that it can automatically generate the corresponding URL address based on the current URL mode and settings. The format is:

U('地址','参数','伪静态','是否跳转','显示域名');
Copy after login

in the template The advantage of using the U method instead of hard-coding the URL is that once your environment changes or parameter settings change, you don't need to change any code in the template.

The calling format in the template needs to be {:U('address', 'parameter'...)}

Basic usage

Usage examples 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, you can also just write the operation name to indicate calling the current module's

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

In addition to grouping, module and operation names, 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 incoming parameters and supports 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

However, the following definition method is not allowed 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, such as:

U('Blog/read?id=1')
Copy after login

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

That’s it Will generate

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 final URL address generated by the same method is:

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

If multiple pseudo-static suffixes are set Static support, 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

The 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 The 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:

Just pass in the domain name that needs to be specified after
U('Blog/read@blog.thinkphp.cn','id=1');
Copy after login

@.

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

Recommended: "The latest 10 thinkphp video tutorials"

The above is the detailed content of How to use thinkphp u method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template