PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

Yii2开发Restful API的时候如何为不同的Module显示各自的错误格式?

原创
2016-08-04 09:19:47 728浏览

框架: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是那个模块,当然这是比较挫的实现方式,如果楼主找到了更好的解决方案,欢迎分享

错误显示格式指的是什么意思?不同的错误页面?

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。