#Yii Handling error in module

This my progress report about learning yii framework recently. This report is intended for myself in order to keep my learning track. Ok this time I’ll report about how to handle error inside Yii module. By default, if you create the Yii application, any http error (404, 403, 500) will be thrown away to the default handler which is in the site/error action. This error handler config can be found on the config/main.

So if we want to give a different error handler inside the different module, we need to reconfigure the module. First add the code below to the module config inside the init() function. In this case the module name is “myModule” and the error handling will be route to defaultController/error.

Yii::app()->setComponents(array(
   'errorHandler'=>array(
      'errorAction'=>'myModule/defaultController/error')
));

This is the code of error action in the defaultController. This code actually as same as found at the siteController since I just copied it from there.

public function actionError(){
 if($error=Yii::app()->errorHandler->error) {
    if(Yii::app()->request->isAjaxRequest)
        echo $error['message'];
    else
        $this->render('error', $error);
 }
}

Then copy the error view from site/error to the module view. In this sample it will be at myModule/views/default/. That’s my report today and happy coding day!!!.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s