Frame: Yii2 ADV
The directory structure is as follows
api/ models/ web/ modules/ v1/ controllers/ ... v2/ controllers/ ... config/ main.php ...
I'm now going to use different error display formats for the V2 version of the API, so I added events to the response component as described in the documentation, on beforeSend
but in practice it was found that setting events only worked for the application component, and the component for module failed to trigger events.
config/main.php
The code is as follows:
return [' id ' = ' app-api ', ' basepath ' = dirname (__dir__), ' Boo Tstrap ' = = [' Log ',], ' modules ' and ' = ' v1 ' and ' = ' class ' = ' Api\modules\v1\module ', ' BasePath ' = ' @app/modules/v1 ', ' components ' and ' []], ' v2 ' and ' = [ ' Class ' = ' Api\modules\v2\module ', ' basepath ' = ' @app/modules/v2 ', ' components ' and ' [ ' Response ' = [' class ' = ' = \yii\web\response::class, ' on before Send ' = = function ($event) {/** @var \yii\web\response $res */$res = $e vent->sender; if (! $res->issuccessful) {//do something here...//... } } ], ], ] ], ...
If you are using document HTTP://WWW.YIIFRAMEWORK.COM/D directly ... The methods in the V1 and V2 two modules, causing the interface that V1 is using to be incompatible with the app.
What if you only want to set response for a separate module?
Reply content:
Frame: Yii2 ADV
The directory structure is as follows
api/ models/ web/ modules/ v1/ controllers/ ... v2/ controllers/ ... config/ main.php ...
I'm now going to use different error display formats for the V2 version of the API, so I added events to the response component as described in the documentation, on beforeSend
but in practice it was found that setting events only worked for the application component, and the component for module failed to trigger events.
config/main.php
The code is as follows:
return [' id ' = ' app-api ', ' basepath ' = dirname (__dir__), ' Boo Tstrap ' = = [' Log ',], ' modules ' and ' = ' v1 ' and ' = ' class ' = ' Api\modules\v1\module ', ' BasePath ' = ' @app/modules/v1 ', ' components ' and ' []], ' v2 ' and ' = [ ' Class ' = ' Api\modules\v2\module ', ' basepath ' = ' @app/modules/v2 ', ' components ' and ' [ ' Response ' = [' class ' = ' = \yii\web\response::class, ' on before Send ' = = function ($event) {/** @var \yii\web\response $res */$res = $e vent->sender; if (! $res->issuccessful) {//do something here...//... } } ], ], ] ], ...
If you are using document HTTP://WWW.YIIFRAMEWORK.COM/D directly ... The methods in the V1 and V2 two modules, causing the interface that V1 is using to be incompatible with the app.
What if you only want to set response for a separate module?
Can do.
Handler is bound to Response::event_before_send in the Module::init () method within the module.php of each module.
You also need to bind [ErrorHandler] that varies by Module in module.php.
Specific reference module and event documentation
YII2 should be not supported, if you want to do this thing, you can consider the error handler to analyze the URL is that module, of course, this is the way to achieve a relatively frustrated, if the landlord found a better solution, welcome to share
What does the error display format mean? A different error page?