Home > Backend Development > PHP Tutorial > ThinkPHP3.1 new feature Action parameter binding_PHP tutorial

ThinkPHP3.1 new feature Action parameter binding_PHP tutorial

WBOY
Release: 2016-07-13 10:24:43
Original
936 people have browsed it

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();
  }
 }

Copy after login
The access addresses of

URL are:

http://serverName/index.php/Blog/read/id/5
http://serverName/index.php/Blog/archive/year/2012/month/03

Copy after login

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
Copy after login

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
Copy after login

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/

Copy after login
Copy after login

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);
  }
Copy after login

In this way, when we visit

http://serverName/index.php/Blog/read/

Copy after login
Copy after login

will be output

id=0

Copy after login

When we visit

http://serverName/index.php/Blog/archive/

Copy after login

, output:

year=2012&month=01
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825373.htmlTechArticleThe Action parameter binding function of ThinkPHP version 3.1 provides parameter binding support for URL variables and operation methods. This This function can make your operation method definition and parameter acquisition clearer, and it is convenient...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template