Home  >  Article  >  PHP Framework  >  ThinkPHP uses U method to automatically generate URL hyperlinks

ThinkPHP uses U method to automatically generate URL hyperlinks

尚
forward
2020-05-06 09:16:073836browse

ThinkPHP uses U method to automatically generate URL hyperlinks

ThinkPHP U method

The U method is a shortcut method built into ThinkPHP that can dynamically generate intelligent URL addresses based on the system URL mode configuration. .

Since ThinkPHP supports a variety of different URL patterns, as well as project grouping patterns, when the deployment environment changes, the URL pattern may sometimes be changed, and the U method is exactly what solves the problem of different environment configurations. The problem of URL unification. In addition to dynamically generating URLs to adapt to system configurations, the U method also has the benefit of automatically adding pseudo-static suffixes.

U Method syntax

U method definition rules are as follows:

('[分组/模块/操作]?参数' [,'参数','伪静态后缀','是否跳转','显示域名'])

The square brackets are optional parameters. If the group, module and If operating, it will default to the current group, current module and current operation.

U method example

U method is a system function of ThinkPHP and can be used directly in operation methods or in templates (see the following text).

Assume that the current index operation is the Index module, generate the list operation URL of the current module:

U('list?cat_id=1&status=1')

The generated URL is a relative link: http://www.5idev.com/index.php /Index/list/cat_id/1/status/1

At the same time, the U method also supports the following two definition formats. The above example is equivalent to the following definition format:

U'list',array('cate_id'=>1,'status'=>1))
U'list','cate_id=1&status=1')

Use U method in template

U method can not only dynamically generate URL addresses in operation methods, but is actually more commonly used to generate hyperlink addresses in templates:

超链接字符

Generate The actual html code after the URL is:

// Pathinfo 模式:
<a href="http://www.5idev.com/lindex.php/blog/list/cat_id/1/status/1">超链接字符</a>

The U method belongs to the ThinkPHP system function and follows the general method of using system functions in templates, that is, used with {: }.

Use variables in the U method

Use the U method in the template. If you want to use variables (usually), you need to use the . connector:

<a href="{:U(&#39;blog/list?cat_id=&#39;.$vo[&#39;cat_id&#39;].&#39;&status=1&#39;)}">超链接字符</a>

Comparison of U method results under various URL modes

The U method adaptively generates different URL formats according to various URL modes, as shown in the above example generated in each mode The URL comparison is as follows:

  • Normal compatibility mode: http://www.5idev.com/index.php?m=Index&a=list&cat_id=1&status=1

  • Pathinfo mode: http://www.5idev.com/index.php/Index/list/cat_id/1/status/1

  • Rewrite mode: http ://www.5idev.com/Index/list/cat_id/1/status/1

  • Rewrite mode, pseudo-static suffix is ​​.html: http://www.5idev. com/Index/list/cat_id/1/status/1.html

  • Rewrite mode, using - delimiter and .html suffix: http://www.5idev.com/Index -list-cat_id-1-status-1.html

Common U method usage examples

// 当前模块 list 操作
U(&#39;list?cat_id=1&status=1&#39;)
// 其他模块操作
U('Blog/read?id=1')        // 生成Blog模块的read操作,并且id为1的URL地址
// 其他分组
U('Admin/User/view?uid=1')    // 生成Admin分组的User模块的view操作的URL地址

Recommended tutorial: "TP5

The above is the detailed content of ThinkPHP uses U method to automatically generate URL hyperlinks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:oschina.net. If there is any infringement, please contact admin@php.cn delete