如何在 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;
}
最佳答案
在站点 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/