thinkphp Learning--controller _action parameter binding

Source: Internet
Author: User

Parameter binding simplifies the definition of a method or even the parsing of a route by directly binding a variable in the URL address as a parameter to the action method.

The parameter binding feature is turned on by default, and the principle is to bind the parameters in the URL (excluding modules, controllers, and operation names) and the parameters in the action method.

To enable the parameter binding feature, first make sure that you turn on the URL_PARAMS_BIND settings:

' Url_params_bind '       = =  true//  URL variable bound to action method as parameter

Binding by variable name

The default parameter binding method is to bind by variable name, for example, we have defined two operation methods for the blog controller Read and archive methods, because the read operation needs to specify an ID parameter, The archive method needs to specify two parameters for the year and month (month), so we can define the following:

namespace Home\controller; UseThink\controller;classBlogControllerextendscontroller{ Public functionRead$id){        Echo' Id= '.$id; }     Public functionArchive$year= ' 2013 ',$month= ' 01 '){        Echo' Year= '.$year.‘ &month= '.$month; }}

Note that the method of operation here does not have specific business logic, just a simple demonstration.

The URL's access addresses are:

http://SERVERNAME/INDEX.PHP/HOME/BLOG/READ/ID/5http://servername/index.php/home /BLOG/ARCHIVE/YEAR/2013/MONTH/11

The ID parameter in two URL addresses and the year and month parameters are automatically bound to the same name as the Read action method and the archive action method.

Variable name binding is not necessarily determined by the access URL, and the routing address can play the same role

The result of the output is in turn:

Id=5Year =2013&month=11

Parameters bound by variable name must match the name of the variable passed in in the URL, but the order of the parameters does not need to be consistent. Other words

http://servername/index.php/home/blog/archive/month/11/year/2013

And the above access results are consistent, the parameter order in the URL and the order of the parameters in the operation method can be arbitrarily adjusted, the key is to ensure that the parameter name is consistent.

The parameter bindings are still valid if accessed using the following URL address:

http://SERVERNAME/INDEX.PHP?S=/HOME/BLOG/READ/ID/5http://Servername/index.php?s =/HOME/BLOG/ARCHIVE/YEAR/2013/MONTH/11http://servername/index.php?c=blog&a=read&id=5 http://servername/index.php?c=blog&a=archive&year=2013&month=11

If the URL address that the user accesses is (as to why this visit is not mentioned):

http://servername/index.php/home/blog/read/

Then the following exception prompt is thrown:参数错误:id

The reason for the error is simple, because the ID parameter must pass in the parameter when the read operation method is executed, but the method cannot get the correct ID parameter information from the URL address. Since we cannot trust any input from the user, it is recommended that you add a default value to the ID parameter of the Read method, for example:

 Public function Read ($id=0) {        echo ' id= '.  $id;    }

This way, when we visit, we http://serverName/index.php/Home/Blog/read/ will output

Id=0

When we visit http://serverName/index.php/Home/Blog/archive/ , the output:

Year=2013&month=01

Always defining default values for the parameters of an action method is a good way to avoid errors

Bind by variable Order

The second way is to bind in the order of the variables, in which case the order of the parameters in the URL address is very important and cannot be arbitrarily adjusted. To bind in the order of variables, you must first set URL_PARAMS_BIND_TYPE to 1:

' Url_params_bind_type '  +  //  Set parameter bindings bind in variable order

The definition of the operation method does not need to change, the URL's access address is changed to:

http://SERVERNAME/INDEX.PHP/HOME/BLOG/READ/5http://servername/index.php/home/ BLOG/ARCHIVE/2013/11

The result of the output is in turn:

Id=5Year =2013&month=11

This time if you change to

http://servername/index.php/home/blog/archive/11/2013

The result of the output becomes:

year=11&month=2013

Obviously there is a problem, so you cannot arbitrarily adjust the order of the parameters in the URL, to ensure that your operation method is defined in the same order.

As you can see, the effect of this parameter binding is somewhat similar to a simple rule route.

Binding by variable order is currently valid only for PathInfo addresses, so the following URL access parameter bindings are invalidated:

http://servername/index.php?c=blog&a=read&id=5http://servername/ Index.php?c=blog&a=archive&year=2013&month=11

However, the compatibility mode URL address access is still valid:

http://SERVERNAME/INDEX.PHP?S=/HOME/BLOG/READ/5http://servername/index.php?s=/ HOME/BLOG/ARCHIVE/2013/11

If your action method definition has no parameters or you do not want to use this function, you can turn off the parameter binding feature:

' Url_params_bind '       =  false

Thinkphp Learning--controller _action parameter binding

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.