yii 自定义错误页面,如 404、403、500 [英] yii custom error pages like 404, 403, 500

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

问题描述

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

公共函数 actionError(){$error = Yii::app()->errorHandler->error;开关($错误['代码']){案例404:$this->render('error404', array('error' => $error));休息;............}}

我想知道是否有更好的方法?或者如果 Yii 有办法处理我遗漏的这个问题.

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

它说明了将文件放入 /protected/views/system 的内容,但我不太了解 Yii 的文档.

解决方案

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

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

默认页面如下所示,您可以根据需要对其进行修改并将其保存在/protected/views/system

<h1>错误 <?php echo $data['code'];?></h1><h2><?php echo nl2br(CHtml::encode($data['message']));?></h2><p>当 Web 服务器处理您的请求时发生上述错误.</p><p>如果您认为这是服务器错误,请联系 <?php echo $data['admin'];?>.</p><p>谢谢你.</p><div class="version"><?php echo date('Y-m-d H:i:s',$data['time']) .''.$data['版本'];?>

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

 code:HTTP 状态码(例如 403、500);type:错误类型(例如 CHttpException、PHP 错误);消息:错误信息;file:发生错误的PHP脚本文件名;line:发生错误的代码行号;trace:错误的调用栈;source:发生错误的上下文源代码.

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

返回数组(......'组件'=>数组('errorHandler'=>数组('errorAction'='站点/错误',),),);

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

I'm trying to have separate files for all my error messages. (404, 403, 500 etc) so i can have custom designs for them. If possible i don't want the header and footer to be included in my error pages too. Right now i have this, in my SiteController.php and put error404.php into my views/site/ folder

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

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

i was wondering if there is a better way? or if Yii has way to handle this that i'm missing.

i read this page http://www.yiiframework.com/doc/guide/1.1/en/topics.error

and it says something about putting files into /protected/views/system but i don't quite understand Yii's documentation.

解决方案

As you read Yii will look for files in /protected/views/system (after looking in themes if you have any)

You do not need to write any fresh actions all you have to do is create a folder system in the views directory and create files named errorXXX.php XXX being the error code.

The default page looks like this you modify this as you wish and save it in /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>

You will have access to following attributes in your $data array

    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.

The alternative technique is how you created an action in SiteController,however to activate this you need to change your main config file and route errors to this action:

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

If you wish to only skin your errors then this is not necessary, if you want to do more complex stuff like on error log to a DB, send a email to the admin etc then it is good to have your own action with additional logic.

这篇关于yii 自定义错误页面,如 404、403、500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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