【问题标题】:yii custom error pages like 404, 403, 500yii 自定义错误页面,如 404、403、500
【发布时间】:2014-05-07 14:57:06
【问题描述】:

我正在尝试为我的所有错误消息创建单独的文件。 (404、403、500 等)所以我可以为它们定制设计。如果可能的话,我不希望页眉和页脚也包含在我的错误页面中。现在我有这个,在我的 SiteController.php 中并将 error404.php 放入我的 views/site/ 文件夹

public function actionError()
        {
                $error = Yii::app()->errorHandler->error;
                switch($error['code'])
                {
                        case 404:

                                $this->render('error404', array('error' => $error));
                                break;
                        .......
                        .......
                }
        }

我想知道是否有更好的方法?或者如果 Yii 有办法处理我所缺少的。

我读了这个页面http://www.yiiframework.com/doc/guide/1.1/en/topics.error

它提到了将文件放入/protected/views/system,但我不太了解 Yii 的文档。

【问题讨论】:

    标签: yii


    【解决方案1】:

    当你阅读时,Yii 会在/protected/views/system 中查找文件(如果有的话,在查看主题之后)

    您不需要编写任何新的操作,您只需在views 目录中创建一个文件夹系统并创建名为errorXXX.php XXX 的文件,即错误代码。

    默认页面如下所示,您可以随意修改并将其保存在/protected/views/system

    <body>
         <h1>Error <?php echo $data['code']; ?></h1>
         <h2><?php echo nl2br(CHtml::encode($data['message'])); ?></h2>
         <p>
            The above error occurred when the Web server was processing your request.
         </p>
         <p>
           If you think this is a server error, please contact <?php echo $data['admin']; ?>.
         </p>
         <p>
            Thank you.
         </p>
         <div class="version">
            <?php echo date('Y-m-d H:i:s',$data['time']) .' '. $data['version']; ?>
         </div>
    </body>
    

    您将可以访问 $data 数组中的以下属性

        code: the HTTP status code (e.g. 403, 500);
        type: the error type (e.g. CHttpException, PHP Error);
        message: the error message;
        file: the name of the PHP script file where the error occurs;
        line: the line number of the code where the error occurs;
        trace: the call stack of the error;
        source: the context source code where the error occurs.
    

    另一种技术是您在 SiteController 中创建操作的方式,但是要激活它,您需要更改主配置文件并将错误路由到该操作:

    return array(
        ......
        'components'=>array(
            'errorHandler'=>array(
                'errorAction'=>'site/error',
            ),
        ),
    );
    

    如果您只想对错误进行皮肤处理,那么这不是必需的,如果您想做更复杂的事情,例如将错误日志记录到数据库,向管理员发送电子邮件等,那么最好有自己的操作附加逻辑。

    【讨论】:

    • 嗯,由于某种原因,当我删除 site/error.php 并将 eror404.php 放入 /protected/views/system 时,它说 SiteController cannot find the requested view "error".
    • 这是因为你配置了errorHandler调用site/error;仍在 SiteController 中的操作正在尝试查找视图文件 error.php 已移动,因此您的错误;您需要删除 errorHandler 指令;
    • 啊好的。知道了。因此,如果我这样做,我将无法将错误记录到 DB。基本上它是一个静态文件?
    • 您当然可以在 DB/file/email 中记录错误,而无需声明自定义操作为此您需要配置错误记录配置,请参阅yiiframework.com/doc/api/1.1/CDbLogRoute 和此yiiframework.com/doc/guide/1.1/en/… 了解更多信息
    • 视图文件未从 /protected/views/system 呈现
    猜你喜欢
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 2013-02-04
    • 2018-07-16
    • 1970-01-01
    • 2011-01-23
    • 2015-08-26
    • 1970-01-01
    相关资源
    最近更新 更多