How do I display the respective error format for different module YII2 when developing the RESTful API?

Source: Internet
Author: User
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.phpThe 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.phpThe 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?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.