Error handling in Yii is different than handling errors in plain PHP. First of all, Yii will convert all non-fatal errors to *exceptions*. By doing so, you can gracefully handle them using `try`-`catch`. Second, even fatal errors in Yii are rendered in a nice way. This means that in debugging mode, you can trace the causes of fatal errors in order to more quickly identify the cause of the problem.
Error handling in Yii is different than handling errors in plain PHP. First of all, Yii will convert all non-fatal errors
to *exceptions*. By doing so, you can gracefully handle them using `try`-`catch`. Second, even fatal errors in Yii are
rendered in a nice way. This means that in debugging mode, you can trace the causes of fatal errors in order to more quickly
identify the cause of the problem.
Rendering errors in a dedicated controller action
Rendering errors in a dedicated controller action
-------------------------------------------------
-------------------------------------------------
The default Yii error page is great when developing a site, and is acceptable for production sites if `YII_DEBUG` is turned off in your bootstrap index.php file. But but you may want to customize the default error page to make it more suitable for your project.
The default Yii error page is great when developing a site, and is acceptable for production sites if `YII_DEBUG`
is turned off in your bootstrap index.php file. But but you may want to customize the default error page to make it
more suitable for your project.
The easiest way to create a custom error page it is to use a dedicated controller action for error rendering. First, you'll need to configure the `errorHandler` component in the application's configuration:
The easiest way to create a custom error page it is to use a dedicated controller action for error rendering. First,
you'll need to configure the `errorHandler` component in the application's configuration:
```php
```php
return[
return[
...
@@ -20,7 +26,8 @@ return [
...
@@ -20,7 +26,8 @@ return [
],
],
```
```
With that configuration in place, whenever an error occurs, Yii will execute the "error" action of the "Site" controller. That action should look for an exception and, if present, render the proper view file, passing along the exception:
With that configuration in place, whenever an error occurs, Yii will execute the "error" action of the "Site" controller.
That action should look for an exception and, if present, render the proper view file, passing along the exception:
```php
```php
publicfunctionactionError()
publicfunctionactionError()
...
@@ -31,22 +38,23 @@ public function actionError()
...
@@ -31,22 +38,23 @@ public function actionError()
}
}
```
```
Next, you would create the `views/site/error.php` file, which would make use of the exception. The exception object has the following properties:
Next, you would create the `views/site/error.php` file, which would make use of the exception. The exception object has
the following properties:
*`code`: the HTTP status code (e.g. 403, 500)
-`statusCode`: the HTTP status code (e.g. 403, 500). Available for HTTP exceptions only.
*`type`: the error type (e.g. CHttpException, PHP Error)
-`code`: the code of the exception.
*`message`: the error message
-`type`: the error type (e.g. HttpException, PHP Error).
*`file`: the name of the PHP script file where the error occurs
-`message`: the error message.
*`line`: the line number of the code where the error occurs
-`file`: the name of the PHP script file where the error occurs.
*`trace`: the call stack of the error
-`line`: the line number of the code where the error occurs.
*`source`: the context source code where the error occurs
-`trace`: the call stack of the error.
-`source`: the context source code where the error occurs.
[[Need to confirm the above for Yii 2.]]
Rendering errors without a dedicated controller action
Rendering errors without a dedicated controller action
Instead of creating a dedicated action within the Site controller, you could just indicate to Yii what class should be used to handle errors:
Instead of creating a dedicated action within the Site controller, you could just indicate to Yii what class should
be used to handle errors:
```php
```php
publicfunctionactions()
publicfunctionactions()
...
@@ -59,7 +67,8 @@ public function actions()
...
@@ -59,7 +67,8 @@ public function actions()
}
}
```
```
After associating the class with the error as in the above, define the `views/site/error.php` file, which will automatically be used. The view will be passed three variables:
After associating the class with the error as in the above, define the `views/site/error.php` file, which will
automatically be used. The view will be passed three variables: