【问题标题】:YII 1 Error and exception handlingYII 1 错误和异常处理
【发布时间】:2016-09-16 05:14:35
【问题描述】:

如何在 yii 中处理 CHttpException。如果有人输入错误的网址,那么我想将用户重定向到特定页面。我可以处理除以 0 错误但无法处理 CHttpException
我在模块>组件>控制器中编写此代码

    public function init()
    {
        parent::init();
        Yii::app()->attachEventHandler('onError',array($this,'handleError'));
        Yii::app()->attachEventHandler('onException',array($this,'handleError'));
    }

    public function handleError(CEvent $event)
    {

        if ($event instanceof CExceptionEvent)
        if ($event instanceof CHttpException)
        {
            // some logics
        }
        elseif($event instanceof CErrorEvent)
        {
            $arr[0]=$event->code;
            $arr[1]=$event->message;

            $this->render("application.views.site.error", array('event' => $arr));
        }
        $event->handled = TRUE;
    }

【问题讨论】:

    标签: url exception yii error-handling exception-handling


    【解决方案1】:

    在站点控制器中:

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

    【讨论】:

    • 先生..!其实问题是如何检测CHttpException?
    • 使用 try{} catch{} 语句检测异常,
    猜你喜欢
    • 2014-04-06
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 2010-11-16
    • 2016-05-17
    相关资源
    最近更新 更多