Skip to content

Response

Overview

Each Skinny application instance has one response object. The response object is an abstraction of your Skinny application’s HTTP response that is returned to the HTTP client. Although each Skinny application includes a default response object, the \Skinny\Http\Response class is idempotent; you may instantiate the class at will (in middleware or elsewhere in your Skinny application) without affecting the application as a whole. You can obtain a reference to the Skinny application’s response object with:

<?php
$app = \Skinny\Skinny::newInstance();
$app->response;

An HTTP response has three primary properties:

  • Status
  • Header
  • Body

The response object provides helper methods, described next, that help you interact with these HTTP response properties. The default response object will return a 200 OK HTTP response with the text/html content type.

Status

The HTTP response returned to the client will have a status code indicating the response’s type (e.g. 200 OK, 400 Bad Request, or 500 Server Error). You can use the Skinny application’s response object to set the HTTP response’s status like this:

<?php
$app->response->setStatus(400);

You only need to set the response object’s status if you intend to return an HTTP response that does not have a 200 OK status. You can just as easily fetch the response object’s current HTTP status like this:

<?php
$status = $app->response->getStatus();

Headers

The HTTP response returned to the HTTP client will have a header. The HTTP header is a list of keys and values that provide metadata about the HTTP response. You can use the Skinny application’s response object to set the HTTP response’s header. The response object has a public property headers that is an instance of \Skinny\Helper\Set; this provides a simple, standardized interface to manipulate the HTTP response headers.

<?php
$app = \Skinny\Skinny::newInstance();
$app->response->headers->set('Content-Type', 'application/json');

You may also fetch headers from the response object’s headers property, too:

<?php
$contentType = $app->response->headers->get('Content-Type');

If a header with the given name does not exist, null is returned. You may specify header names with upper, lower, or mixed case with dashes or underscores. Use the naming convention with which you are most comfortable.

Body

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

<?php
$app = \Skinny\Skinny::newInstance();

// Overwrite response body
$app->response->setBody('Foo');

// Append response body
$app->response->write('Bar');

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

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

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

If output buffering is enabled (the default), you will usually not need to manually set the response body with the setBody() or write() methods; instead, the Skinny app will do this for you. Whenever you echo content inside a route’s callback function, the content is captured in an output buffer and appended to the response body before the HTTP response is returned to the client.

Cookies

The Skinny application provides helper methods to send cookies with the HTTP response.

This example demonstrates how to use the Skinny application’s setCookie() method to create an HTTP cookie to be sent with the HTTP response:

<?php
$app->setCookie('foo', 'bar', '2 days');

This creates an HTTP cookie with the name “foo” and value “bar” that expires two days from now. You may also provide additional cookie properties, including its path, domain, secure, and httponly settings. The Skinny application’s setCookie() method uses the same signature as PHP’s native setCookie() function.

<?php
$app->setCookie(
    $name,
    $value,
    $expiresAt,
    $path,
    $domain,
    $secure,
    $httponly
);

You can tell Skinny to encrypt the response cookies by setting the app’s cookies.encrypt setting to true. When this setting is true, Skinny will encrypt the response cookies automatically before they are returned to the HTTP client.

Here are the available Skinny app settings used for cookie encryption:

<?php
$app = \Skinny\Skinny::newInstance(array(
    'cookies.encrypt' => true,
    'cookies.secret_key' => 'my_secret_key',
    'cookies.cipher' => 'aes-256-cbc',
));

You can delete a cookie using the Skinny application’s deleteCookie() method. This will remove the cookie from the HTTP client before the next HTTP request. This method accepts the same signature as the Skinny application’s setCookie() instance method, without the $expires argument. Only the first argument is required.

<?php
$app->deleteCookie('foo');

If you need to also specify the path and domain:

<?php
$app->deleteCookie('foo', '/', 'foo.com');

You may also further specify the secure and httponly properties:

<?php
$app->deleteCookie('foo', '/', 'foo.com', true, true);

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 is an integer; the header is an iterable data structure; and the body is a string. Were you to create a new \Skinny\Http\Response object in your Skinny application or its middleware, you would call the response object’s finalize() method to produce the status, header, and body for the underlying HTTP response.

<?php
/**
 * Prepare new response object
 */
$res = new \Skinny\Http\Response();
$res->setStatus(400);
$res->write('You made a bad request');
$res->headers->set('Content-Type', 'text/plain');

/**
 * Finalize
 * @return [
 *     200,
 *     ['Content-type' => 'text/plain'],
 *     'You made a bad request'
 * ]
 */
$array = $res->finalize();

Redirect

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

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

Status Introspection

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

<?php
$res = $app->response;

//Is this an informational response?
$res->isInformational();

//Is this a 200 OK response?
$res->isOk();

//Is this a 2xx successful response?
$res->isSuccessful();

//Is this a 3xx redirection response?
$res->isRedirection();

//Is this a specific redirect response? (301, 302, 303, 307)
$res->isRedirect();

//Is this a forbidden response?
$res->isForbidden();

//Is this a not found response?
$res->isNotFound();

//Is this a client error response?
$res->isClientError();

//Is this a server error response?
$res->isServerError();