How do I override/modify the REST serializer in Yii2?

I’m building a REST API with Yii2. Normally the request response looks something like this:


    {

    "items": [

        {

            "id": 1,

            ...

        },

        {

            "id": 2,

            ...

        },

        ...

    ],

    "_links": {

        "self": {

            "href": "http://localhost/users?page=1"

        },

        "next": {

            "href": "http://localhost/users?page=2"

        },

        "last": {

            "href": "http://localhost/users?page=50"

        }

    },

    "_meta": {

        "totalCount": 1000,

        "pageCount": 50,

        "currentPage": 1,

        "perPage": 20

    }

}

I want to override the serializer so that the fields contained in the "_meta" array are instead included in the root of the array, i.e. the same level as "items" and "_links". How and where do I do that?

Thank you.

Extend the base serializer with yours, then extend the rest controller and have it use your serializer.

Thank you. Where do I place the new serializer?

Anywhere you want. Typically it’s going into app\components namespace and a corresponding directory.

That worked perfectly. Thank you!