title: Why is Yii2 rest controller giving response in XML format?
body: <p>Currently I am using the following initialization code on my api module</p>
  
  <pre><code>public function init()
  {
      parent::init();
      Yii::$app-&gt;response-&gt;format = Response::FORMAT_JSON;
  }
  </code></pre>
  
  <p>My api gives back response in XML format in the following example.</p>
  
  <pre><code>public function actionTest()
  {
      $items = ['one', 'two', 'three' =&gt; ['a', 'b', 'c']];
      return $items;
  }
  </code></pre>
  
  <p>This is the response:</p>
  
  <pre><code>&lt;response&gt;
    &lt;item&gt;one&lt;/item&gt;
    &lt;item&gt;two&lt;/item&gt;
     &lt;three&gt;
      &lt;item&gt;a&lt;/item&gt;
      &lt;item&gt;b&lt;/item&gt;
      &lt;item&gt;c&lt;/item&gt;
     &lt;/three&gt;
  &lt;/response&gt;
  </code></pre>
  
  <p><strong>The only way I can get it to work is by adding this line to each of the controller behaviors.</strong> I have read the documentation which says I can initalize this on the module class so I do not need to do it in every controller. I do not know why it gives XML though. <strong>Also</strong> just in case the only way is to add it to my behaviors, do I have to write code to handle name, code, status, type, previous and code or does Yii provide yii\rest\Controller and yii\rest\ActiveController which automatically handle this. Clearly they are being output automatically when there is an error. </p>
  
  <pre><code>{"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"
   }
  }
  </code></pre>