Home > Backend Development > PHP Tutorial > How to display error formats for different Modules when developing Restful API in Yii2?

How to display error formats for different Modules when developing Restful API in Yii2?

WBOY
Release: 2016-08-04 09:19:47
Original
992 people have browsed it

Framework: Yii2 Adv
The directory structure is as follows

<code>api/
    models/
    web/
    modules/
        v1/
            controllers/
            ...
        v2/
            controllers/
            ...
    config/
        main.php
        ...</code>
Copy after login
Copy after login

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.phpThe 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>
Copy after login
Copy after login

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?

Reply content:

Framework: Yii2 Adv
The directory structure is as follows

<code>api/
    models/
    web/
    modules/
        v1/
            controllers/
            ...
        v2/
            controllers/
            ...
    config/
        main.php
        ...</code>
Copy after login
Copy after login

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.phpThe 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>
Copy after login
Copy after login

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?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template