如何处理yii中的CHttpException。如果有人输入了错误的网址,那么我想将用户重定向到特定网页。我可以处理除以0错误但无法handel 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;
}
答案 0 :(得分:0)
在站点控制器中:
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);
}
}