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

ThinkPHP uses U method to automatically generate URL hyperlinks

Release: 2020-05-06 09:16:07
forward
3902 people have browsed it

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:

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

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

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

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:

超链接字符
Copy after login

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

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

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

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!

Related labels:
source:oschina.net
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