How to redirect in afterAction under component controller class "CController"

Hello guys.

I need your help to resolve my issue. I’m stuck here from approx 3-4 hours.

I made custom roles and permissions to every user. I have executed the code under component controller class’s function afterAction. But if user don’t have the access of the clicked action then it should be redirect to error page. When i use redirect function it says “Cannot modify header information - headers already sent”. My code is below here :




if (isset(yii::app()->user->id)) {

            $controller = yii::app()->controller->id;

            $action = yii::app()->controller->action->id;


            $noAuthControllerAction = array();

            $noAuthControllerAction[] = 'site/index';


            $controllerAction = $controller . '/' . $action;


            if (!in_array($controllerAction, $noAuthControllerAction)) {

                $isAllowed = $this->isAllowed($controller, $action);

                if (!$isAllowed) {

                    $this->redirect(array('site/denied'));

                }

            }

        }

        parent::afterAction($action);



This happens when output is written before the redirect. So make sure nothing is echoed before the redirect.

Or make sure that your files are encoded with UTF8 without BOM (if you use UTF8)

Thank you for your response. But code is in front of you. I did nothing echo here. I don’t get the issue even after so many time. If you have any working example please post here. Thanks in advance.

I would recomend to put the logic in beforeAction and call


if (!$isAllowed) {

  $this->redirect(array('site/denied'));

  Yii::app()-end();

}