Skip to content

Commit b524117

Browse files
committed
Fixes #168: Added "Overriding error handler from a module" recipe
1 parent 17fc7ef commit b524117

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

book/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Logging and error handling
1818
- Customizing 404 page
1919
- Handling errors and exceptions
2020
- Understanding error stack trace
21+
- [Overriding error handler from a module](overriding-error-handler-from-a-module.md)
2122

2223
Web essentials
2324
--------------
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Overriding error handler from a module
2+
3+
In some cases you want a custom error handler to be used for particular module.
4+
5+
Yii doesn't have support for module-based error handling. The error handler is
6+
global i.e. there's only one for the whole application that is already registered
7+
by the time a module is executed. Therefore we need to both overwrite `errorHandler`
8+
component, set it for a current application and register it as a PHP error handler.
9+
Here's how to do it:
10+
11+
```php
12+
class Module extends \yii\base\Module
13+
{
14+
public function init()
15+
{
16+
parent::init();
17+
\Yii::configure($this, [
18+
'components' => [
19+
'errorHandler' => [
20+
'class' => ErrorHandler::className(),
21+
]
22+
],
23+
]);
24+
25+
/** @var ErrorHandler $handler */
26+
$handler = $this->get('errorHandler');
27+
\Yii::$app->set('errorHandler', $handler);
28+
$handler->register();
29+
}
30+
}
31+
```
32+

0 commit comments

Comments
 (0)