Action parameter binding _php instance of ThinkPHP3.1 new feature

Source: Internet
Author: User

The ThinkPHP3.1 version of the action parameter bindings feature provides parameter binding support for URL variables and operation methods, which enables you to define the method of operation and to obtain clearer parameters, and to invoke the Operation method across modules. This new feature has no effect on the use of previous methods of operation, and you can also change the way you used to define your previous operations.

The principle of the action parameter binding is to bind the parameters in the URL (excluding grouping, module, and Operation address) and the parameters in the Controller's action method. For example, we have defined two action methods read and archive methods for the blog module, and the archive method needs to specify the year and month (month) two parameters because the read operation requires an ID parameter.

Class Blogaction extends action{public
  function Read ($id) {
    echo ' id= '. $id;
    $Blog = M (' Blog ');
    $Blog->find ($id);
  }
  Public Function archive ($year = ', $month = ') {
    echo ' year= '. $year. ' &month= '. $month;
    $Blog = M (' Blog ');
    $year  =  $year;
    $month =  $month;
    $begin _time = Strtotime ($year. $month. "a");
    $end _time = Strtotime ("+1 month", $begin _time);
    $map [' create_time '] = array (' GT ', $begin _time), array (' LT ', $end _time));
    $map [' status '] =  1;
    $list = $Blog->where ($map)->select ();
  }
 

The URL's access address is:

HTTP://SERVERNAME/INDEX.PHP/BLOG/READ/ID/5
http://serverName/index.php/Blog/archive/year/2012/month/03

The ID parameters in the two URL addresses and the year and month parameters are automatically bound to the read action method and the parameter with the same name as the archive action method.
The results of the output in turn are:

Id=5
year=2012&month=03

The arguments for the action parameter must match the parameter names passed in 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 parameters in the URL in the order and operation methods in the order of the parameters can be arbitrarily adjusted, the key is to ensure that the parameter name consistent.
If the user accesses the URL address (as to why this visit is not mentioned):

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

The following exception hint is thrown:

Parameter error: ID

The reason for the error is simple, because the ID parameter must pass in the parameter when the Read action 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:

 The 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

Id=0

When we visit

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

The time, the output:

Year=2012&month=01

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.