ThinkPHP uses the U method to automatically generate URL hyperlinks

Source: Internet
Author: User
The U method is a built-in shortcut of ThinkPHP. you can dynamically generate a smart URL address based on the system URL mode. ThinkPHP U method

The U method is a built-in shortcut of ThinkPHP. you can dynamically generate intelligent URLs based on the system URL mode configuration.
Address.

ThinkPHP supports different URL modes and project grouping modes. Therefore, when the deployment environment changes, the URL mode may be changed.
The U method solves the unified URL problem in different environment configurations. In addition to dynamically generating URLs for adaptive system configuration, the U method also has the advantage of automatically adding pseudo-static suffixes.
U
Method syntax

The U method is defined as follows:

('[Group/module/operation]? Parameter'
[, 'Parameter', 'pseudo-static suffixes ', 'redirect?', 'display domain name'])

An optional parameter is provided in square brackets. if the group, module, and operation are not specified, the current group, current module, and current operation are used by default.
U method instance

U
As a system function of ThinkPHP, the method can be directly used in the operation method or in the template (see the text below ).

Assume that it is the Index of the index module.
Operation to generate the list operation URL of the current module:

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

Generated URL
For relative link: http://www.phpo.com/index.php/Index/list/cat_id/1/status/1

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

U'list', array ('Cate _ id' => 1, 'status' => 1 ))
U 'list', 'Cate _ id = 1 & status = 1 ')

Used in the template
U method

The U method can not only dynamically generate URL addresses in the operation method, but also generate hyperlink addresses in the template:

Href = "{: U ('list? Cat_id = 1 & status = 1')} "> hyperlink character

The actual
The html code is:

// Pathinfo mode:
Href = "http://www.phpo.com/lindex.php/blog/list/cat_id/1/status/1"> hyperlink character

U
The method is a ThinkPHP system function. It follows the general method of using system functions in the template, that is, it is used together with {:}. for details, see "using functions in the ThinkPHP template".
U
Use variables in the method

The U method used in the template. if you want to use a variable (normally), you need to use the. Connector:

Href = "{: U ('blog/list? Cat_id = '. $ vo ['cat _ id'].' & status = 1')} "> hyperlink

Various
Comparison of the U method results in URL mode

The U method generates different URL formats according to various URL modes. for example
Comparison:

Common compatible mode: http://www.phpo.com/index.php? M = Index & a = list & cat_id = 1 & status = 1
Pathinfo
Mode: http://www.phpo.com/index.php/Index/list/cat_id/1/status/1
Rewrite
Mode: http://www.phpo.com/Index/list/cat_id/1/status/1
Rewrite mode. the pseudo-static suffix is
. Html: http://www.phpo.com/Index/list/cat_id/1/status/1.html
Rewrite mode, use-
Separator with. html suffix: http://www.phpo.com/Index-list-cat_id-1-status-1.html

Common U
Example




  1. // List the current module
  2. U ('list? Cat_id = 1 & status = 1 ')
  3. // Other module operations
  4. U ('blog/read? Id = 1') // Generate the read operation of the Blog module, and the URL address with id 1
  5. // Other groups
  6. U ('admin/User/view? Uid = 1') // Generate the URL of the view operation of the User module of the Admin group

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.