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.
- Class BlogAction extends Action {
- Public function read ($ id ){
- Echo 'Id = '. $ id;
- $ Blog = M ('blog ');
- $ Blog-> find ($ id );
- }
-
- Public function archive ($ year = '20140901', $ 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 ('GT ', $ begin_time), array ('Lt', $ end_time ));
- $ Map ['status'] = 1;
- $ List = $ Blog-> where ($ map)-> select ();
- }
- }
The access addresses of the Copy code URL are:
- Http: // serverName/index. php/Blog/read/id/5
- 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:
- Id = 5
- 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
- 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 ):
- 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:
- Public function read ($ id = 0 ){
- Echo 'Id = '. $ id;
- $ Blog = M ('blog ');
- $ Blog-> find ($ id );
- }
Copy the code so that when we access
- Http: // serverName/index. php/Blog/read/
When the code is copied, the output is
- Id = 0
Copy the code when we access
- Http: // serverName/index. php/Blog/archive/
When the code is copied, the output is:
- Year = 2012 & month = 01
Copy code