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
- // List the current module
- U ('list? Cat_id = 1 & status = 1 ')
- // Other module operations
- U ('blog/read? Id = 1') // Generate the read operation of the Blog module, and the URL address with id 1
- // Other groups
- U ('admin/User/view? Uid = 1') // Generate the URL of the view operation of the User module of the Admin group