title: Combine two JSON responses together in CakePHP
body: <p>In my CakePHP app I do some stuff in the beforeFilter to pass extra JSON data with the response about the user or authentication etc.</p>
  
  <p>For example:</p>
  
  <pre><code>public function beforeFilter()
  {
      $this-&gt;Auth-&gt;allow(array('index'));
  
      // If the request is either JSON or XML
      if ( $this-&gt;params['ext'] == 'json' || $this-&gt;params['ext'] == 'xml' )
      {
          if( $this-&gt;Auth-&gt;user() )
          {
              // User is logged in so send userdata and usual response will also be passed
              $response = array('meta' =&gt; 
                  array(
                      'auth' =&gt; true,
                      'user_id' =&gt; $this-&gt;Auth-&gt;user('id')
                  )
              );
              echo json_encode($response);
          }
          else
          {
              $requiresAuth = !in_array($this-&gt;params['action'], $this-&gt;Auth-&gt;allowedActions);
              if ($requiresAuth)
              {
                  $response = array('meta' =&gt; 
                      array(
                          'auth' =&gt; false
                      )
                  );
                  echo json_encode($response);
                  exit; // exit so no other responses go through
              }
              else
              {
                  // Send JSON usual response
                  $response = array('meta' =&gt; 
                      array(
                          'auth' =&gt; false
                      )
                  );
                  echo json_encode($response);
              }
          }
      }
  
      parent::beforeFilter();
  
  }
  </code></pre>
  
  <p>This is used to build my RESTful API for a mobile app. The issue here is that the full returned JSON looks like this:</p>
  
  <pre><code>{
      "meta": {
          "auth": false,
      }
  } {
      "posts": [{
          "Post": {
              "id": "136",
              "user_id": "8",
              "datetime": "2012-09-11 15:49:52",
              "modified": "2012-09-16 15:31:38",
              "title": "Where is good to eat in New York?",
              "slug": "Where_is_good_to_eat_in_New_York",
              "content": "Preferably italian or mexican and with a good atmosphere.\r\n\r\nAlso within the **Manhattan** area!",
              "status": "1",
              "promoted": "0",
              "latitude": "53.6570794",
              "longitude": "-1.8277604"
          },...
  </code></pre>
  
  <p>As you can see the two JSON objects don't combine together properly... How do I fix this?</p>
  
  <p>Using CakePHP 2.3</p>