Fundamentals Action Help

Hello,

I am reading all the docs and am failing to understand this section: http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action

Can some one give a real world example as to why you would override the action?? i am having trouble wrapping my head around why you would do:




class PostController extends CController

{

    public function actions()

    {

        return array(

            'edit'=>'application.controllers.post.UpdateAction',

        );

    }

}



instead of




class PostController extends CController

{

    public function actionUpdate()

    {

        //update logic here

    }

}



Anyway I am sure this is a total newb question, but a real world example/usage case would help me understand it…

Thanks!

The first form (action as separate class) is very convenient if the action contains some unified code and can be reused by many controllers.

For example, yii has CCaptchaAction which can be used together with CCapthcaValidator to implement captcha feature.

Seb has already well answered your question.

I also use this technique to isolate actions that require quite a lot of lines of code to be executed so I keep my controller class code cleaner and easier to read.

Nevertheless, when you do what I said if you only extend your action class to that of CAction you wont be able to reach parent controller’s actions (render, renderPartial) without getting an instance of its controller - $controller=$this->getController()). For simplicity of my code I use the extension XRenderAction class for that purpose.