Error Handling In Controller Init()

Hi

I have multilingual site

I put MlController in /components


class MlController extends Controller

{

        init(){

        // If there is a post-request, redirect the application to the provided url of the selected language

        if(isset($_POST['language']))

        {

            $lang = $_POST['language'];

            $MultilangReturnUrl = $_POST[$lang];

            $this->redirect($MultilangReturnUrl);

        }

        // Set the application language if provided by GET, session or cookie

        if(isset($_GET['language']))

        {

            Yii::app()->language = $_GET['language'];

            Yii::app()->user->setState('language', $_GET['language']);

            $cookie = new CHttpCookie('language', $_GET['language']);

            $cookie->expire = time() + (60*60*24*365); // (1 year)

            Yii::app()->request->cookies['language'] = $cookie;

        }

        else

        {

            if(!Yii::app()->request->pathInfo)

            {

                if (Yii::app()->user->hasState('language'))

                {

                    Yii::app()->language = Yii::app()->user->getState('language');

                }

                else if(isset(Yii::app()->request->cookies['language']))

                {

                    Yii::app()->language = Yii::app()->request->cookies['language']->value;

                }

                else

                {

                    Yii::app()->language=Yii::app()->params['defaultLanguage'];

                }

                $this->redirect('/'.Yii::app()->language);

            }

            Yii::app()->language=Yii::app()->params['defaultLanguage'];

            throw new CHttpException(404,'The page can not be found.');

        }

    }

} 

In main.cfg i have


        'errorHandler'=>array(

            'errorAction'=>'site/error',

        ), 

But thorowing of exception doesn’t execute SiteController actionError()

I simply get


CHttpException


The page can not be found. (Y:\home\...\public_html\protected\components\MlController.php:47)


#0 Y:\home\...\public_html\framework\web\CWebApplication.php(282): MlController->init()

#1 Y:\home\...\public_html\framework\base\CErrorHandler.php(332): CWebApplication->runController('site/error')

#2 Y:\home\...\public_html\framework\base\CErrorHandler.php(205): CErrorHandler->render('error', Array)

#3 Y:\home\...\public_html\framework\base\CErrorHandler.php(130): CErrorHandler->handleException(Object(CHttpException))

#4 Y:\home\...\public_html\framework\base\CApplication.php(713): CErrorHandler->handle(Object(CExceptionEvent))

#5 [internal function]: CApplication->handleException(Object(CHttpException))

#6 {main}

What is wrong?

P.S. For example in




class NewsController extends MlController

{

    public function loadModel($id, $alias)

    {

        $model=News::model()->find("`id`=:id AND `alias`=:alias", array('id'=>$id, 'alias'=>$alias));


        if($model===null)

        {

            throw new CHttpException(404,'The requested news does not exist.');

        }

        return $model;

    } 

}



Error handling works properly

I get this executed


class SiteController extends MlController

{

    public function actionError()

    {

        if($error=Yii::app()->errorHandler->error)

        {

            if(Yii::app()->request->isAjaxRequest)

                echo $error['message'];

            else

                $this->render('error', $error);

        }

    }

}

Hi gv0zd

you have write code “throw new CHttpException(404,‘The page can not be found.’);” in init(),

So before sitecontroller achieved to load (sitecontroller extends MlController) exception already raised

How do expect the siteController catch the exception event and root to site/error?

(but I am not sure 100% that is the only problem) :)

you can see in callstack that the error is thrown when rendering "error" action:

CErrorHandler->render(‘error’, Array) => CWebApplication->runController(‘site/error’)

it is because SiteController also extends MlController, so before "error" action is called your init handler is executed and throws exception… you should consider putting error handling in separate controller that does not extend MlController.

Thanks!!!