質問

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top