title: Yii2 activeDataProvider pagination and additional data
body: <p>I am building an API but am stuck trying to add additional data to a result set. </p>
  
  <p>I am using ActiveDataProvider in a class that extends from ActiveControler to get and return the result set is JSON. It works so well and includes the pagination headers without any work required.</p>
  
  <pre><code>$provider = new \yii\data\ActiveDataProvider([
      'query' =&gt; Message::find()-&gt;where(['location_id' =&gt; $id])
  ]);
  
  return $provider;
  </code></pre>
  
  <p>However if I get the results first so that I can add new values, the pagination headers are no longer sent back to the client.</p>
  
  <pre><code>$provider = new \yii\data\ActiveDataProvider([
      'query' =&gt; Message::find()-&gt;where(['location_id' =&gt; $id])
  ]);
  
  $messages['messages'] = $provider-&gt;getModels();
  $messages['additional_stuff'] = ['field1' =&gt; 'foo', 'field2' =&gt; 'bar'];
  return $messages;
  </code></pre>
  
  <p>An example of what I might want the output to look like could be:</p>
  
  <pre><code>{
      "messages": [
        {
          "id": "1",
          "content": "message 1"
        },
        {
          "id": "2",
          "content": "message 2"
        },
       ],
       "additional_stuff": {
           "field1": "foo",
           "field2": "bar"
       }
  }
  </code></pre>
  
  <p>What can I do here to add additional values but ensure that the pagination headers are sent?</p>
  
  <p>EDIT:</p>
  
  <p>Thanks to devOp's response I was able to add the headers as required. My action now looks like this:</p>
  
  <pre><code>$provider = new \yii\data\ActiveDataProvider([
      'query' =&gt; Message::find()-&gt;where(['location_id' =&gt; $id])
  ]);
  
  $messages['messages'] = $provider-&gt;getModels();
  $messages['additional_stuff'] = ['field1' =&gt; 'foo', 'field2' =&gt; 'bar'];
  
  $this-&gt;sendPaginationHeaders($provider-&gt;getHeaders());
  
  return $messages;
  </code></pre>
  
  <p>Which calls in my new method: </p>
  
  <pre><code>protected function sendPaginationHeaders($pagination)
  {
      $pageCount = $pagination-&gt;totalCount &lt;= $pagination-&gt;getPageSize() ? 1 : ceil($pagination-&gt;totalCount / $pagination-&gt;getPageSize());
  
      header('X-Pagination-Current-Page: '. $pagination-&gt;getPage());
      header('X-Pagination-Page-Count: '. $pageCount);
      header('X-Pagination-Per-Page: '. $pagination-&gt;getPageSize());
      header('X-Pagination-Total-Count: '. $pagination-&gt;totalCount);
  }
  </code></pre>
  
  <p>Which adds the standard Yii2 pagination headers. Unfortunately getHeaders() didn't give me the Page-Count but it's easy to calculate that from the others.</p>