Yii Framework Forum: Error Handling In Controller Init() - Yii Framework Forum

Jump to content

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

Error Handling In Controller Init() Rate Topic: -----

#1 User is offline   gv0zd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 06-April 12

Posted 04 December 2012 - 06:34 AM

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);
        }
    }
}

0

#2 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,311
  • Joined: 21-February 11
  • Location:Greece

Posted 04 December 2012 - 09:17 AM

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) :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#3 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 773
  • Joined: 02-July 10
  • Location:Central Poland

Posted 04 December 2012 - 09:35 AM

View Postgv0zd, on 04 December 2012 - 06:34 AM, said:

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}



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.
red
1

#4 User is offline   gv0zd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 06-April 12

Posted 04 December 2012 - 09:48 AM

View Postredguy, on 04 December 2012 - 09:35 AM, said:

you should consider putting error handling in separate controller that does not extend MlController.

Thanks!!!
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