php YII 如何处理自定义 404 错误页面以及其他错误页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11815543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:10:42  来源:igfitidea点击:

YII how to handle custom 404 error page along with other error pages

phpyiihttp-status-code-404

提问by wolvorinePk

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

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

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

默认情况下,我有 Sitecontroller,它包含如下错误操作功能

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',
    ),

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

我的问题是我只需要自定义 404 页面,其余的错误我需要处理它由 sitecontroller 的错误函数处理的方式。但我找不到办法做到这一点。如果假设我从配置主中删除“errorAction”=>“site/error”,那么它确实通过调用显示 404 错误

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

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.

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

采纳答案by Boris Belenski

Use this code for actionError:

将此代码用于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 );
}

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

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

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

Error handler works like this: when httpexception arise, component will check if there is any value in errorActionproperty and if there is any, will run this controller's action. If there is no set value to the errorActionproperty, 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.

错误处理程序是这样工作的:当 httpexception 出现时,组件将检查errorAction属性中是否有任何值,如果有,将运行该控制器的操作。如果errorAction属性没有设置值,将显示系统文件夹中的错误视图。所以不需要混合来自系统视图文件夹和控制器视图文件夹的错误视图。

回答by bingjie2680

Whenever errors occur, the action error in siteController is called. you can customize the error route in that action, you can do something like this:

每当发生错误时,都会调用 siteController 中的操作错误。您可以在该操作中自定义错误路由,您可以执行以下操作:

if(404==Yii::app()->errorHandler->error->code){
     //go to custome error page
else
   //code default error.php

回答by DMor

Can't you do this with .htaccess? Personally I create an "errors" folder with all the html php files that holds the error messages and modify .htaccess to call those pages while coming across the error.

你不能用 .htaccess 做到这一点吗?我个人创建了一个“错误”文件夹,其中包含所有保存错误消息的 html php 文件,并在遇到错误时修改 .htaccess 以调用这些页面。

Links:

链接

http://www.javascriptkit.com/howto/htaccess2.shtml

http://www.javascriptkit.com/howto/htaccess2.shtml

http://www.addedbytes.com/for-beginners/error-documents-for-beginners/

http://www. addedbytes.com/for-beginners/error-documents-for-beginners/

Examples:

例子

Create a .htaccess file in the directory you want the error pages to be called and in the text file, write the following line:

在要调用错误页面的目录中创建一个 .htaccess 文件,并在文本文件中写入以下行:

ErrorDocument 404     /404.html

assuming there is a page called 404.html in the same directory, when a 404 page not found error is genrated, the 404.html page will be called.

假设在同一个目录下有一个叫404.html的页面,当出现404 page not found错误时,会调用404.html页面。

The same works with other error codes:

同样适用于其他错误代码:

ErrorDocument 500     /500error.html

assuming a 500 error was created and a 500error.html file exists in the same directory.

假设创建了 500 错误并且 500error.html 文件存在于同一目录中。

回答by croppio.com

In the latest versions of the framework (I am working with 1.14) use:

在最新版本的框架(我使用 1.14)中使用:

Yii::app()->errorHandler->error['code']

because erroris an array.

因为error是一个数组。