thinkphp function: U method

Source: Internet
Author: User
Tags subdomain

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:

    1. U(‘User/add‘) // 生成User模块的add操作地址

复制代码

You can also support grouping calls:

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

    1. U(‘add‘) // 生成当前访问模块的add操作地址

复制代码

In addition to grouping, modules, and operation names, we can also pass in some parameters:

    1. 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:

    1. U(‘Blog/cate‘,array(‘cate_id‘=>1,‘status‘=>1))
    2. U(‘Blog/cate‘,‘cate_id=1&status=1‘)
    3. U(‘Blog/cate?cate_id=1&status=1‘)

复制代码

However, it is not allowed to use the following definition to pass parameters:

    1. 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:

    1. 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:

    1. 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:

    1. 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:

    1. http://serverName/Blog/read/id/1

复制代码

If you also set the PathInfo delimiter:

    1. ‘URL_PATHINFO_DEPR‘=>‘_‘

复制代码

Will generate

    1. 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:

    1. 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:

    1. U(‘Blog/read‘,‘id=1‘,‘xml‘)

复制代码

Will generate

    1. http://serverName/Blog/read/id/1.xml

复制代码

The route support U method can also support routing if we define a routing rule for:

    1. ‘news/:id\d‘=>‘News/read‘

复制代码

Then you can use

    1. U(‘/news/1‘)

复制代码

The resulting URL address is:

    1. 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:

    1. 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:

    1. U(‘Blog/read#comment‘,‘id=1‘,‘html‘)

复制代码

Will generate

    1. 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:

    1. U(‘Blog/read#[email protected]‘,‘id=1‘);

thinkphp function: U method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.