File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ Logging and error handling
18
18
- Customizing 404 page
19
19
- Handling errors and exceptions
20
20
- Understanding error stack trace
21
+ - [ Overriding error handler from a module] ( overriding-error-handler-from-a-module.md )
21
22
22
23
Web essentials
23
24
--------------
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments