Home > Article > Backend Development > How to display error formats for different Modules when developing Restful API in Yii2?
Framework: Yii2 Adv
The directory structure is as follows
<code>api/ models/ web/ modules/ v1/ controllers/ ... v2/ controllers/ ... config/ main.php ...</code>
Now I plan to use a different error display format for the v2 version of the API, so I added the on beforeSend
event to the response component as described in the document, but in practice I found that setting the event in this way can only work for application components, and for Module The component cannot trigger the event.config/main.php
The code is as follows:
<code>return [ 'id' => 'app-api', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log',], 'modules' => [ 'v1' => [ 'class' => 'api\modules\v1\Module', 'basePath' => '@app/modules/v1', 'components' => [ ] ], 'v2' => [ 'class' => 'api\modules\v2\Module', 'basePath' => '@app/modules/v2', 'components' => [ 'response' => [ 'class' => \yii\web\Response::class, 'on beforeSend' => function ($event) { /** @var \yii\web\Response $res */ $res = $event->sender; if (!$res->isSuccessful) { // do something here... // ... } } ], ], ] ], ...</code>
If you directly use the method in the document http://www.yiiframework.com/d..., it will work on both v1 and v2 modules, causing the interface being used by v1 to be incompatible with the App.
If you only plan to use Is there any other way to implement the Response setting in a separate Module?
Framework: Yii2 Adv
The directory structure is as follows
<code>api/ models/ web/ modules/ v1/ controllers/ ... v2/ controllers/ ... config/ main.php ...</code>
Now I plan to use a different error display format for the v2 version of the API, so I added the on beforeSend
event to the response component as described in the document, but in practice I found that setting the event in this way can only work for application components, and for Module The component cannot trigger the event.config/main.php
The code is as follows:
<code>return [ 'id' => 'app-api', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log',], 'modules' => [ 'v1' => [ 'class' => 'api\modules\v1\Module', 'basePath' => '@app/modules/v1', 'components' => [ ] ], 'v2' => [ 'class' => 'api\modules\v2\Module', 'basePath' => '@app/modules/v2', 'components' => [ 'response' => [ 'class' => \yii\web\Response::class, 'on beforeSend' => function ($event) { /** @var \yii\web\Response $res */ $res = $event->sender; if (!$res->isSuccessful) { // do something here... // ... } } ], ], ] ], ...</code>
If you directly use the method in the document http://www.yiiframework.com/d..., it will work on both v1 and v2 modules, causing the interface being used by v1 to be incompatible with the App.
If you only plan to use Is there any other way to implement the Response setting in a separate Module?
It can be done.
In the Module::init() method in Module.php of each module, just bind the handler to Response::EVENT_BEFORE_SEND.
You also need to bind the [errorHandler] that varies from module to module in Module.php.
Please refer to the module and event documentation for details
Yii2 should not support it. If you want to do this, you can consider analyzing which module the url is in the error handler. Of course, this is a relatively frustrating way to implement it. If the author finds a better solution, welcome to share it
What does the error display format mean? Different error pages?