R
R
Roman2015-05-25 15:49:12
Yii
Roman, 2015-05-25 15:49:12

How can I change the Json output (structure)?

I want to change the output of all Returns

{
  "success": true,
  "payload": {
    /* Application-specific data would go here. */
    Тут возвращать модель $model  
  }
}

Output errors like this
{
  "success": false,
  "payload": {
    /* Application-specific data would go here. */
  },
  "error": {
   Тут $model->errors
  }
}

Since the output structure is different when there are errors with validation or something else. I want to wrap in one wrapper.
How can you do?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman, 2015-05-25
@xr0m3oz

They helped in the chat ( https://gitter.im/yiisoft/yii2/rus ) and posted it to the forum, who is interested
yiiframework.ru/forum/viewtopic.php?f=30&t=29318&p...

M
Mikhail Osher, 2015-05-25
@miraage

Inherit controllers from some BaseController. In it, create a method that will receive a model, something else - and output the result in the desired format.
Alternatively, create a trait.

A
Alexey Volegov, 2015-05-25
@EagleMoor

Go to yii2.
Read about www.yiiframework.com/doc-2.0/guide-rest-response-f...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question