The basic execution unit of the Yii Controller is action. generally, an actionMe function is defined in the Controller class. when you access the action of me (see Yii analysis 5: routing management class UrlManager and Yii Analysis 7: Execution of runController ),
The basic execution unit of the Yii Controller is action. generally, an actionMe function is defined in the Controller class. when you access the action of me (see Yii analysis 5: route Management class UrlManager and Yii Analysis 7: Execution of runController), the actionMe method is automatically executed. In the actual condition, if the Controller has multiple actions, if all the action processing logic is written in the Controller, the Controller class will be abnormal, which is not conducive to later maintenance, we can configure the action map by overwriting the actions method to distribute the inaccessible actions to various classes for processing:
PHPpublic function actions () {return array ('action1' => array ('class' => 'path. to. actionclass1 ', 'properties' => '',), 'action2' => array ('class' => 'path. to. actionclass2 ', 'properties' => '',),);}
123456789101112 |
Public function actions () {return array ('action1' => array ('class' => 'path. to. actionclass1 ', 'properties' => '',), 'action2' => array ('class' => 'path. to. actionclass2 ', 'properties' => '',),);} |
After the preceding configuration array is defined, the Controller first checks whether the actionDeal method exists for an action named 'dest, if you do not determine whether the returned value array of actions has a value whose key is deal, use the configured class for processing, this action class must have at least one run method (not necessarily inherit the CAction class) to execute the corresponding processing logic. Otherwise, a fatal error is reported.
Although this action class does not have to inherit the CAction class as long as there is a run method, we recommend that you use the CAction class. On the one hand, we maintain the integrity of the framework, and on the other hand, we cannot access and call its Controller. The CAction code is simple:
PHP // This is an abstract class CAction extends CComponent implements IAction {private $ _ id; private $ _ controller;/*** Constructor used to create an action class for the parent class, at the same time, the controler is saved as a parameter in the member * @ param CController $ controller the controller who owns this action. * @ param string $ id of the action. */public function _ construct ($ controller, $ id) {$ this-> _ controller = $ controller; $ this-> _ id = $ id ;} /*** @ return CController returns the controller with this action */public function getController () {return $ this-> _ controller ;} /*** @ return returns the action id */public function getId () {return $ this-> _ id ;}}
123456789101112131415161718192021222324252627282930313233 |
// This is an abstract class CAction extends CComponent implements IAction {private $ _ id; private $ _ controller;/*** Constructor used to create an action class for the parent class, at the same time, the controler is saved as a parameter in the member * @ param CController $ controller the controller who owns this action. * @ param string $ id of the action. */public function _ construct ($ controller, $ id) {$ this-> _ controller = $ controller; $ this-> _ id = $ id ;} /*** @ return CController returns the controller with this action */public function getController () {return $ this-> _ controller ;} /*** @ return returns the action id */public function getId () {return $ this-> _ id ;}} |