YII如何处理自定义404错误页面以及其他错误页面 [英] YII how to handle custom 404 error page along with other error pages

查看:216
本文介绍了YII如何处理自定义404错误页面以及其他错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示404错误页面,因为我已在我的protected/view/system文件夹中创建了error404.php文件.

I want to display 404 error page for that i have made error404.php file in my protected/view/system folder.

默认情况下,我具有Sitecontroller,并且其中包含以下错误操作功能

By default i have Sitecontroller and it contained error action function as below

public function actionError()
{
    if($error=Yii::app()->errorHandler->error)
    {

        if(Yii::app()->request->isAjaxRequest)
            echo $error['message'];
        else
            $this->render('error', $error);
    }
}

在主配置文件中,它被定义为

inside main config file it is defined as

    'errorHandler'=>array(
        // use 'site/error' action to display errors
        'errorAction'=>'site/error',
    ),

我的问题是我只需要自定义404页面,其余的错误则需要处理sitecontroller的错误功能正在处理的方式.但是我找不到办法.如果假设我从config main中删除了'errorAction'=>'site/error',则它会通过调用

my problem is i need to customize 404 page only, rest of the error i need to handle the way it is being handle by sitecontroller's error function. But i could not find a way to do that. If suppose i remove 'errorAction'=>'site/error', from the config main then it does show the 404 error by calling

        throw new CHttpException(404, 'Page not found');

但是这样做我只能看到没有布局的页面,其他自定义错误也被视为与404相同,而没有.我阅读了很多次手册,但仍然无法解决.

but doing that i can only see the page without layout also other custom errors are treated same as 404 while they are not. I read the manual many times but i still cant able to resolve it.

推荐答案

将此代码用于 actionError :

$app = Yii::app();
if( $error = $app->errorHandler->error->code )
{
    if( $app->request->isAjaxRequest )
        echo $error['message'];
    else
        $this->render( 'error' . ( $this->getViewFile( 'error' . $error ) ? $error : '' ), $error );
}

在视图/站点中,为404错误创建 error404.php ,为其余部分创建 error.php .

In views/site create error404.php for 404 errors and error.php for the rest.

或者您可以在配置中为您想以不同方式处理的错误定义参数,并针对该错误检查错误代码:

Or you can define param in the config for errors you like to handle differently and check error code against it:

$app = Yii::app();
if( $error = $app->errorHandler->error->code )
{
    if( Yii::app()->request->isAjaxRequest )
        echo $error['message'];
    else    
        $this->render( 'error' . ( in_array( $error, $app->params[ 'customErrorPages' ] ) ? $error : '' ), $error );
}

错误处理程序的工作方式如下:当出现httpexception时,组件将检查 errorAction 属性中是否有任何值,如果有,将运行此控制器的操作.如果 errorAction 属性没有设置值,将显示系统文件夹中的错误视图.因此,无需混合来自系统视图文件夹和控制器视图文件夹的错误视图.

Error handler works like this: when httpexception arise, component will check if there is any value in errorAction property and if there is any, will run this controller's action. If there is no set value to the errorAction property, will display error view from system folder. So there is no need to mix error views from system view folder and controller's view folder.

这篇关于YII如何处理自定义404错误页面以及其他错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆