yii custom error pages like 404, 403, 500
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.
user2636556
Updated on June 28, 2022Comments
-
user2636556 almost 2 years
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. -
user2636556 almost 10 yearshmm for some reason when i remove
site/error.php
and put eror404.php into/protected/views/system
it saysSiteController cannot find the requested view "error".
-
Manquer almost 10 yearsThis is because you have configured the errorHandler to call site/error; the action still being in the SiteController is trying to find the view file error.php which is moved hence your error; you need to remove the errorHandler directive;
-
user2636556 almost 10 yearsah ok. got it. so if i do it this way i can't do error logging to DB. basically its a static file?
-
Manquer almost 10 yearsyou can of course log errors in DB/file/email without declaring custom actions For that you need to configure your error logging config see yiiframework.com/doc/api/1.1/CDbLogRoute and this yiiframework.com/doc/guide/1.1/en/… for more info
-
Raviranjan Mishra over 5 yearsView files are not getting rendered from /protected/views/system