The ThinkPHP3.1 version of the Action parameter binding feature provides parameter binding support for URL variables and action methods that can make your operation method definitions and parameter fetching clearer, and also facilitates the invocation of action methods across modules. This new feature has no effect on previous methods of operation, and you can also change the definition of the previous operation method in a different way.
The action parameter binding is done by binding the parameters in the URL (excluding the grouping, module, and Operation address) and the parameters in the Controller's action method. For example, we have defined two methods of operation for the blog module, the 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).
Class Blogaction extends action{public function Read ($id) { echo ' id= '. $id; $Blog = M (' Blog '); $Blog->find ($id); } Public Function archive ($year = ' a ', $month = ' "") { echo ' year= '. $year. ' &month= '. $month; $Blog = M (' Blog '); $year = $year; $month = $month; $begin _time = Strtotime ($year. $month. "n"); $end _time = Strtotime ("+1 month", $begin _time); $map [' create_time '] = Array (array (' GT ', $begin _time), array (' LT ', $end _time)); $map [' status '] = 1; $list = $Blog->where ($map)->select (); } }
The URL's access addresses are:
Http://serverName/index.php/Blog/read/id/5http://serverName/index.php/Blog/archive/year/2012/month/03
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.
The result of the output is in turn:
id=5year=2012&month=03
The argument bound by the action parameter must match the name of the parameter passed in the URL, but the parameter order does not need to be consistent. Other words
http://serverName/index.php/Blog/archive/month/03/year/2012
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.
If the URL address that the user accesses is (as to why this visit is not mentioned):
http://serverName/index.php/Blog/read/
Then the following exception prompt is thrown:
Parameter error: 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; $Blog = M (' Blog '); $Blog->find ($id); }
This way, when we visit
http://serverName/index.php/Blog/read/
It will output when the
Id=0
When we visit
http://serverName/index.php/Blog/archive/
, the output:
Year=2012&month=01