Helpers

The response object provides helper methods to inspect and interact with the underlying HTTP response.

Finalize

The response object’s finalize() method returns a numeric array of [status, header, body]. The status isan integer; the header is an iterable data structure; and the body is a string. Were you to create a new\Slim\Http\Response object in your Slim application or its middleware, you would call the response object’sfinalize() method to produce the status, header, and body for the underlying HTTP response.

  1. <?php
  2. /**
  3. * Prepare new response object
  4. */
  5. $res = new \Slim\Http\Response();
  6. $res->setStatus(400);
  7. $res->write('You made a bad request');
  8. $res->headers->set('Content-Type', 'text/plain');
  9. /**
  10. * Finalize
  11. * @return [
  12. * 200,
  13. * ['Content-type' => 'text/plain'],
  14. * 'You made a bad request'
  15. * ]
  16. */
  17. $array = $res->finalize();

Redirect

The response object’s redirect() method will set the response status and its Location: header needed toreturn a 3xx Redirect response.

  1. <?php
  2. $app->response->redirect('/foo', 303);

Status Introspection

The response object provides other helper methods to inspect its current status. All of the following methodsreturn a boolean value:

  1. <?php
  2. $res = $app->response;
  3. //Is this an informational response?
  4. $res->isInformational();
  5. //Is this a 200 OK response?
  6. $res->isOk();
  7. //Is this a 2xx successful response?
  8. $res->isSuccessful();
  9. //Is this a 3xx redirection response?
  10. $res->isRedirection();
  11. //Is this a specific redirect response? (301, 302, 303, 307)
  12. $res->isRedirect();
  13. //Is this a forbidden response?
  14. $res->isForbidden();
  15. //Is this a not found response?
  16. $res->isNotFound();
  17. //Is this a client error response?
  18. $res->isClientError();
  19. //Is this a server error response?
  20. $res->isServerError();