Bind the Action parameter of the new ThinkPHP3.1 feature. The Action parameter binding function of ThinkPHP3.1 provides support for binding URL variables and parameters of the operation method. this function makes the definition of your operation method and parameter retrieval clearer, the Action parameter binding function of ThinkPHP3.1 also provides support for binding URL variables and parameters of operation methods. This function can make the definition of operation methods and parameters clearer, it is also convenient to call operation methods across modules. This new feature has no impact on the usage of previous operation methods. you can also modify the definition of previous operation methods in a new way.
The Action parameter binding principle is to bind the parameters in the URL (excluding the group, module, and Operation address) to the parameters in the controller's operation method. For example, we have defined two operation methods for the Blog module: read and archive. because the read operation requires an id parameter, the archive method requires specifying the year and month) two parameters.
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(); } }
The URL access addresses are:
http://serverName/index.php/Blog/read/id/5http://serverName/index.php/Blog/archive/year/2012/month/03
The id and year and month parameters in the two URLs are automatically bound to the same name parameters of the read and archive operation methods.
The output result is as follows:
id=5year=2012&month=03
The parameter bound to the Action parameter must be the same as the parameter name entered in the URL, but the parameter order does not need to be the same. That is to say
http://serverName/index.php/Blog/archive/month/03/year/2012
The preceding access results are the same. the parameter order in the URL and the parameter order in the operation method can be adjusted at will. The key is to ensure that the parameter names are consistent.
If the URL you access is (why is this access not mentioned at the moment ):
http://serverName/index.php/Blog/read/
The following exception prompt will be thrown:
Parameter error: id
The error is reported because the id parameter must be input during the read operation, but the method cannot obtain the correct id parameter information from the URL address. Because we cannot trust any user input, we recommend that you add the 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 access
http://serverName/index.php/Blog/read/
Will output
id=0
When we access
http://serverName/index.php/Blog/archive/
Output:
year=2012&month=01
...