title: Slim Framework middleware array json manipulation
body: <p>I am trying to implement a simple middleware in Slim Framework that adds an array element in the response body. Without the middleware I am getting correct result as <code>{"mytest":"running"}</code>. What I really want is to have the middle ware merge another element and make it as <code>{"mytest":"running","MODE":"development"}</code>. Instead, I am getting this result <code>{"0":"{\"mytest\"","1":"\"running\"}","MODE":"development"}</code> .</p>
  
  <p>I am definitely missing something very simple. Please have a look below for the code I am using. I guess I am not able to convert the body to an array.</p>
  
  <p>This is what I am doing:</p>
  
  <h3>index.php</h3>
  
  <pre><code>require 'Vendor/Slim/Slim.php';
  \Slim\Slim::registerAutoloader();
  
  $app = new \Slim\Slim();
  
  //Middleware
  $app-&gt;add(new \Slim\Middleware\TestMiddleware());
  
   // Middleware Test
      $app-&gt;get( 
          '/mid',
          function () use ($app) {
            $response = $app-&gt;response();
            $response-&gt;body(json_encode(array(
                  'mytest'=&gt;'running'
                  )));
  
          }
      );
  </code></pre>
  
  <h3>TestMiddleware.php</h3>
  
  <p>
  
  <pre><code>namespace Slim\Middleware;
  
  //Appends mode to the response bpoy
  class TestMiddleware extends \Slim\Middleware
  {
      public function call()
      {
          $app=$this-&gt;app;
  
          $this-&gt;next-&gt;call();
  
          $res=$app-&gt;response;
          $body=$res-&gt;getBody();
          $res-&gt;setBody(
              json_encode(
                       array_merge(
                         explode(":",$body),array(
                  'MODE'=&gt;'development'
              )))
          );
  
      }
  
  }
  </code></pre>