Tuesday, April 16, 2013

How do I call a method before every controller action yii using beforeAction

http://www.yiiframework.com/doc/api/1.1/CController#beforeAction-detail

protected function beforeAction(CAction $action)
{
    return true;
}

If you create this in your main Controller under components then you can call by:
return parent::beforeAction($action);

"protected/controllers/MyController.php"
<?php
protected function beforeAction(CAction $action)
{
    if(parent::beforeAction($action)) {
        return true;
    }
    return false;
}
?>

"protected/components/Controller.php"
<?php
protected function beforeAction(CAction $action)
{  
    require_once "protected/Utility/General.php"
    /* Do some common work here if you need in common controller */
    return true;
}
?>

No comments:

Post a Comment