Yii Framework Forum: Fundamentals Action Help - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Fundamentals Action Help Rate Topic: -----

#1 User is offline   MyJqueryLife 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 21-October 10

Posted 23 October 2010 - 08:48 PM

Hello,

I am reading all the docs and am failing to understand this section: http://www.yiiframew...ntroller#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!
0

#2 User is offline   seb 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 240
  • Joined: 29-June 09

Posted 24 October 2010 - 04:59 AM

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.
0

#3 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 24 October 2010 - 06:28 AM

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.
┬┐How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users