I am currently using the following initialization code for my api module
public function init() { parent::init(); Yii::$app->response->format = Response::FORMAT_JSON; }
My api returns an XML response in the following example.
public function actionTest() { $items = ['one', 'two', 'three' => ['a', 'b', 'c']]; return $items; }
This is the answer:
<response> <item>one</item> <item>two</item> <three> <item>a</item> <item>b</item> <item>c</item> </three> </response>
The only way I can get it to work is to add this line to every controller behavior. I read the documentation that says that I can initialize this in a module class, so I do not need to do this in every controller. I do not know why this gives XML. Also , if the only way to add it to my behavior, I need to write code to handle the name, code, status, type, previous and code, or Yii to provide yii \ rest \ Controller and yii \ rest \ ActiveController that automatically handle this. Obviously, they are automatically displayed when an error occurs.
{"name":"Not Found" "message":"Page not found.", "code":0, "status":404 "type":"yii\\web\\NotFoundHttpException" "previous":{"name":"Invalid Route","message":"Unable to resolve the request: api/home/", "code":0,"type":"yii\\base\\InvalidRouteException" } }
source share