A Brief Analysis of the Use of U Method in ThinkPHP

  • 2021-06-29 10:41:53
  • OfStack

The rules for defining the U method in thinkPHP are as follows (parameters in square brackets are determined by practical application):

U ('[Item:/][Route@][Groupname-Module/] Operation?Parameter 1 = Value 1 [ & Parameter N=value N]')
Or you can pass in parameters as arrays:
U ('[Item: //][Route@][Grouping Name-Module/]Operation', array ('Parameter 1'= > 'Value 1'[,'Parameter N'= > 'Value N'])

If you don't define a project and module, you will see the current project and module names. Here are some simple examples:

U ('Myapp://User/add')//URL address of the add operation that generated the User module of the Myapp project
U ('Blog/read?id=1')//read operation that generates an Blog module and id is the URL address of 1
U ('Admin-User/select')//URL address of the select operation that generates the User module grouped by Admin

Make sure to use the parameters?id=1 & name=tp or the way arrays are defined, although in some cases U ('Blog/read/id/1') and U ('Blog/read?id=1') have the same effect, they can cause parsing errors under different URL settings.

Depending on the URL settings of your project, the same U method call can intelligently correspond to different URL address effects, such as for the definition of U ('Blog/read?id=1').

If the current URL is set to normal mode, the resulting URL address is:
HTUhttp:// < serverName > /index.php?m=Blog & a=read & id=1UTH

If URL is currently set to PATHINFO mode, the URL address generated by the same method is:
HTUhttp:// < serverName > /index.php/Blog/read/id/1UTH

If URL is currently set to REWRITE mode, the URL address generated by the same method is:
HTUhttp:// < serverName > /Blog/read/id/1UTH

If the current URL is set to REWRITE mode and the pseudo-static suffix is set to.html, the URL address generated by the same method is:
HTUhttp:// < serverName > /Blog/read/id/1.htmlUTH

In addition, the U method can support routing. If we define a route named View that points to the read operation of the Blog module and the parameter is id, then U ('View@)?id=1') The generated URL address is:
HTUhttp:// < serverName > /index.php/View/id/1UT

Note: Routing support for the U method only supports simple routes, not general and regular routes.


Related articles: