Body

The HTTP response returned to the client will have a body. The HTTP body is the actual content of the HTTP responsedelivered to the client. You can use the Slim application’s response object to set the HTTP response’s body:

  1. <?php
  2. $app = new \Slim\Slim();
  3. // Overwrite response body
  4. $app->response->setBody('Foo');
  5. // Append response body
  6. $app->response->write('Bar');

When you overwrite or append the response object’s body, the response object will automatically set theContent-Length header based on the bytesize of the new response body.

You can fetch the response object’s body like this:

  1. <?php
  2. $body = $app->response->getBody();

Usually, you will never need to manually set the response body with the setBody() or write() methods; instead,the Slim app will do this for you. Whenever you echo() content inside a route’s callback function, theecho()’d content is captured in an output buffer and appended to the response body before the HTTP responseis returned to the client.