框架:yii2 adv
目录结构如下
api/    models/    web/    modules/        v1/            controllers/            ...        v2/            controllers/            ...    config/        main.php        ...
现在打算针对v2版本的api使用不同的错误显示格式, 所以我按照文档上说明的对response组件添加了on beforesend事件, 但是实践中发现这样设置事件只能够对应用组件起作用, 对于module的组件无法触发事件.
config/main.php代码如下:
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...//                            ...                        }                    }                ],            ],        ]    ],    ...
如果直接采用文档http://www.yiiframework.com/d...中的方法会同时对v1和v2两个模块起作用, 导致v1正在使用的接口与app不兼容.
如果只打算对单独的module设置response还有什么实现方式呢?
                                                                                                                                                                                                 回复内容:                                                                                  框架:yii2 adv
目录结构如下
api/    models/    web/    modules/        v1/            controllers/            ...        v2/            controllers/            ...    config/        main.php        ...
现在打算针对v2版本的api使用不同的错误显示格式, 所以我按照文档上说明的对response组件添加了on beforesend事件, 但是实践中发现这样设置事件只能够对应用组件起作用, 对于module的组件无法触发事件.
config/main.php代码如下:
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...//                            ...                        }                    }                ],            ],        ]    ],    ...
如果直接采用文档http://www.yiiframework.com/d...中的方法会同时对v1和v2两个模块起作用, 导致v1正在使用的接口与app不兼容.
如果只打算对单独的module设置response还有什么实现方式呢?
可以做到。
在每个模块的 module.php 内的 module::init() 方法中 给 response::event_before_send 绑定handler即可。
还需要在 module.php 中绑定因 module 而异的 [errorhandler].
具体参考 module 和 event 文档
yii2应该是不支持的,如果要做到这个事情,可以考虑在error handler中去分析url是那个模块,当然这是比较挫的实现方式,如果楼主找到了更好的解决方案,欢迎分享
错误显示格式指的是什么意思?不同的错误页面?
   
 
   