Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
576d4e8d
Commit
576d4e8d
authored
Nov 22, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added "Using controller action to render errors"
parent
2e459092
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
0 deletions
+49
-0
error.md
docs/guide/error.md
+49
-0
No files found.
docs/guide/error.md
View file @
576d4e8d
...
...
@@ -5,3 +5,52 @@ Error handling in Yii is different from plain PHP. First of all, all non-fatal e
you can use
`try`
-
`catch`
to work with these. Second, even fatal errors are rendered in a nice way. In debug mode that
means you have a trace and a piece of code where it happened so it takes less time to analyze and fix it.
Using controller action to render errors
----------------------------------------
Default Yii error page is great for development mode and is OK for production if
`YII_DEBUG`
is turned off but you may
have an idea how to make it more suitable for your project. An easiest way to customize it is to use controller action
for error rendering. In order to do so you need to configure
`errorHandler`
component via application config:
```
php
return
[
// ...
'components'
=>
[
// ...
'errorHandler'
=>
[
'errorAction'
=>
'site/error'
,
],
```
After it is done in case of error Yii will launch
`SiteController::actionError()`
. Since errors are converted to
exceptions we can get exception from error handler:
```
php
public
function
actionError
()
{
$exception
=
\Yii
::
$app
->
getErrorHandler
()
->
exception
;
$this
->
render
(
'myerror'
,
[
'message'
=>
$exception
->
getMessage
()]);
}
```
Since most of the time you need to adjust look and feel only, Yii provides
`ErrorAction`
class that can be used in
controller instead of implementing action yourself:
```
php
public
function
actions
()
{
return
[
'error'
=>
[
'class'
=>
'yii\web\ErrorAction'
,
],
];
}
```
After defining
`actions`
in
`SiteController`
as shown above you can create
`views/site/error.php`
. In the view there
are three varialbes available:
-
`$name`
: the error name
-
`$message`
: the error message
-
`$exception`
: the exception being handled
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment