Yii Analysis 8: Controller execution class CAction

Source: Internet
Author: User
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 ;}}

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.