url - YII 1 错误和异常处理

标签 url exception yii error-handling exception-handling

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

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

enter image description here

最佳答案

在站点 Controller 中:

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

关于url - YII 1 错误和异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37321268/

相关文章:

php - Yii 将数组保存到模型属性

c - 如何在 C 中的不同条件下打开不同的 URL(在网络浏览器中)?

Django url 国际化在生产模式下不起作用

.net - 如何知道 System.Data.Sqlite 对象何时/什么异常被抛出?

javascript - Yii:将数据从 php 传递到 JS 的最佳实践

php - 仅从 foreach 返回唯一值

excel - 代码不会从某些 URL 中提取图像

html - 是否需要 anchor 元素的 URL 编码 "href"属性?

c++ - 子系统:native中的异常处理

c++ - 尝试从 std::runtime_error 继承时出现编译错误