The Action parameter binding function of ThinkPHP version 3.1 provides parameter binding support for URL variables and operation methods. This function can make your operation method definition and parameter acquisition clearer, and also facilitates calling operation methods across modules. This new feature has no impact on the use of previous operation methods. You can also use new methods to transform previous operation method definitions.
The principle of Action parameter binding is to bind the parameters in the URL (excluding groups, modules and operation addresses) with the parameters in the controller's operation method. For example, we have defined two operation methods read and archive method for the Blog module. Since the read operation needs to specify an id parameter, the archive method needs to specify two parameters: year (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='2012',$month='01'){ echo 'year='.$year.'&month='.$month; $Blog = M('Blog'); $year = $year; $month = $month; $begin_time = strtotime($year . $month . "01"); $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(); } }
URL are:
http://serverName/index.php/Blog/read/id/5 http://serverName/index.php/Blog/archive/year/2012/month/03
The id parameter and the year and month parameters in the two URL addresses will automatically be bound to the parameters of the same name in the read operation method and the archive operation method.
The output results are:
id=5 year=2012&month=03
The parameters bound to the Action parameter must have the same name as the parameter passed in the URL, but the order of the parameters does not need to be the same. That is to say
http://serverName/index.php/Blog/archive/month/03/year/2012
The access results are consistent with the above. The order of parameters in the URL and the order of parameters in the operation method can be adjusted at will. The key is to ensure that the parameter names are consistent.
If the URL address visited by the user is (not to mention why it is accessed in this way):
http://serverName/index.php/Blog/read/
Then the following exception prompt will be thrown:
Parameter error: id
The reason for the error is very simple, because when executing the read operation method, the id parameter must be passed in, but the method cannot obtain 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); }
In this way, when we visit
http://serverName/index.php/Blog/read/
will be output
id=0
When we visit
http://serverName/index.php/Blog/archive/
, output:
year=2012&month=01