ThinkPHP3.1 New feature: binding the Action parameter

Source: Internet
Author: User
The Action parameter binding function allows you to bind URL variables to parameters of the operation method. this function makes the definition of the operation method and the acquisition of 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 function allows you to bind URL variables to parameters of the operation method. this function makes the definition of the operation method and the acquisition of 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.
  1. Class BlogAction extends Action {
  2. Public function read ($ id ){
  3. Echo 'Id = '. $ id;
  4. $ Blog = M ('blog ');
  5. $ Blog-> find ($ id );
  6. }
  7.  
  8. Public function archive ($ year = '20140901', $ month = '01 '){
  9. Echo 'Year = '. $ year.' & month = '. $ month;
  10. $ Blog = M ('blog ');
  11. $ Year = $ year;
  12. $ Month = $ month;
  13. $ Begin_time = strtotime ($ year. $ month. "01 ");
  14. $ End_time = strtotime ("+ 1 month", $ begin_time );
  15. $ Map ['create _ time'] = array ('GT ', $ begin_time), array ('Lt', $ end_time ));
  16. $ Map ['status'] = 1;
  17. $ List = $ Blog-> where ($ map)-> select ();
  18. }
  19. }
The access addresses of the Copy code URL are:
  1. Http: // serverName/index. php/Blog/read/id/5
  2. Http: // serverName/index. php/Blog/archive/year/2012/month/03
The id and year and month parameters in the two URL addresses of the Copy code are automatically bound to the same name parameters of the read and archive operation methods.
The output result is as follows:
  1. Id = 5
  2. Year = 2012 & month = 03
The parameter bound to the copy code 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
  1. Http: // serverName/index. php/Blog/archive/month/03/year/2012
The copied code is consistent with the above access results. 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 ):
  1. Http: // serverName/index. php/Blog/read/
If you copy the code, the following exception is 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:
  1. Public function read ($ id = 0 ){
  2. Echo 'Id = '. $ id;
  3. $ Blog = M ('blog ');
  4. $ Blog-> find ($ id );
  5. }
Copy the code so that when we access
  1. Http: // serverName/index. php/Blog/read/
When the code is copied, the output is
  1. Id = 0
Copy the code when we access
  1. Http: // serverName/index. php/Blog/archive/
When the code is copied, the output is:
  1. Year = 2012 & month = 01
Copy code

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.