Reputation: 1265
I have set up error handler into my Yii site.
Here is the code.
main.php
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
),
SiteController.php
public function actionError() {
if ($error = Yii::app()->errorHandler->error) {
if (Yii::app()->request->isAjaxRequest)
echo $error['message'];
else
$this->render('error', $error);
}
}
site/error.php
<?php
$this->pageTitle=Yii::app()->name . ' - Error';
$this->breadcrumbs=array(
'Error',
);
?>
<h2>Error <?php echo $code; ?></h2>
<div class="error">
<?php echo CHtml::encode($message); ?>
</div>
It is working fine and redirect to this custom error page If URL is wrong or User call wrong function.
For example, If In site controller I have set contact
function and User calls URL like this localhost.com/site/contactsdffe
then It redirects to this custom error page.
But the problem is here.
If controller function calls the view contact and I have not set up views/site/contact.php view then It is not called this custom error page. It gives me a this exception error.
CException : SiteController cannot find the requested view "contact".
I don't know why error handler is not working for this scenario and Please let me know that How I can show custom error page for this kind of error.
Any help will appreciate. Thanks in Advance !
Upvotes: 3
Views: 1544
Reputation: 14860
To use site/error
for CException
switch to production mode by setting YII_DEBUG
to false in your entry script (probably the index.php
in your project root).
To use a custom exception page add a custom view exception.php
into your protected/views/system
folder.
From the Yii guide page on errors (emphasis mine):
Yii defines three exception classes:
CException
,CDbException
andCHttpException
.CException
is a generic exception class.CDbException
represents an exception that is caused by some DB-related operations.CHttpException
represents an exception that should be displayed to end users and carries astatusCode
property representing an HTTP status code. The class of an exception determines how it should be displayed, as we will explain next.When an error is forwarded to the
CErrorHandler
application component, it chooses an appropriate view to display the error. If the error is meant to be displayed to end users, such as aCHttpException
, it will use a view namederrorXXX
, whereXXX
stands for the HTTP status code (e.g. 400, 404, 500). If the error is an internal one and should only be displayed to developers, it will use a view namedexception
. In the latter case, complete call stack as well as the error line information will be displayed.When the application runs in production mode, all errors including those internal ones will be displayed using view
errorXXX
. This is because the call stack of an error may contain sensitive information. In this case, developers should rely on the error logs to determine what is the real cause of an error.CErrorHandler searches for the view file corresponding to a view in the following order:
WebRoot/themes/ThemeName/views/system
: this is the system view directory under the currently active theme.
WebRoot/protected/views/system
: this is the default system view directory for an application.
yii/framework/views
: this is the standard system view directory provided by the Yii framework.
In the first instance, Yii throws a CHttpException
during resolution of the url and returns a HTTP status of 404. For the second instance, the url resolves correctly so an internal error occurs and the CException
is thrown.
Upvotes: 3