HTTP Responses

Introduction

All of your WordPress and Router controllers should return a PSR7 compliant response to be sent back to the user's browser. Lumberjack provides a number of Response types which should cover the majority of cases.
If your Controller returns a string it will be automatically converted to an HtmlResponse object with a 200 status code.

Available Response Objects

Timber Response

The most common use-case will be to render a Twig view with some associated data. Using Timber on it's own, your code would look like this:
1
use Timber\Timber;
2
3
...
4
5
return Timber::render('home.twig', $context);
Copied!
In Lumberjack, you should take advantage of the Rareloop\Lumberjack\Http\Responses\TimberResponse object to achieve the same thing:
1
use Rareloop\Lumberjack\Http\Responses\TimberResponse;
2
3
...
4
5
return new TimberResponse('home.twig', $context);
Copied!

Context Flattening

When passing context data to TimberResponse, any objects that implement the Rareloop\Lumberjack\Contracts\Arrayable contract will automatically be flattened to a standard PHP array. This means that it is safe to use objects such as Collection and ViewModel in your data without it causing issues with Twig.

Redirect Response

Redirecting to a different URL can be done by returning an instance of Rareloop\Lumberjack\Http\Responses\RedirectResponse.
1
use Rareloop\Lumberjack\Http\Responses\RedirectResponse;
2
3
...
4
5
return new RedirectResponse('/another/page');
Copied!

Adding Flash Data

If you want to redirect to a URL and also flash some data to the session, you can use the with() method.
1
return new RedirectResponse('/another/page')
2
->with('error', 'Something went wrong');
Copied!

Diactoros Responses

Lumberjack also includes the fantastic Zend Diactoros package which provides additional PSR7 compliant Response Objects.
1
// Check out their documentation for further details and examples:
2
// https://zendframework.github.io/zend-diactoros/
3
$response = new Zend\Diactoros\Response\TextResponse('Hello world!');
4
$response = new Zend\Diactoros\Response\HtmlResponse($htmlContent);
5
$response = new Zend\Diactoros\Response\XmlResponse($xml);
6
$response = new Zend\Diactoros\Response\JsonResponse($data);
7
$response = new Zend\Diactoros\Response\EmptyResponse(); // Basic 204 response:
Copied!

Adding Status Code & Headers

One of the benefits of using Response Objects is that they make it easier to control the HTTP status code & headers.
All the Response Objects bundled with Lumberjack let you set both the status code and headers in their constructor.
1
use Zend\Diactoros\Response\JsonResponse;
2
3
return new JsonResponse($data, 422, ['X-Total-Validation-Errors' => 2]);
Copied!
Or if you prefer, you can use the withStatus and withHeader methods instead.
1
use Zend\Diactoros\Response\JsonResponse;
2
3
return (new JsonResponse($data, 422))
4
->withStatus(422)
5
->withHeader('X-Total-Validation-Errors', 2);
Copied!

Responsable Objects

In addition to supporting PSR7 compliant responses, Controllers can also return an object that implements the Rareloop\Router\Responsable interface. These objects provide a toResponse() method that will return an instance of a PSR7 Response.
1
// app/Http/Controllers/TestController.php
2
namespace App\Http\Controllers;
3
4
use App\Exceptions\TestException;
5
6
class TestController
7
{
8
public function show()
9
{
10
return new TestException('Hello World');
11
}
12
}
13
14
// app/Exceptions/TestException.php
15
namespace App\Exceptions;
16
17
use Rareloop\Router\Responsable;
18
use Psr\Http\Message\RequestInterface;
19
use Psr\Http\Message\ResponseInterface;
20
use Zend\Diactoros\Response\JsonResponse;
21
22
class TestException extends \Exception implements Responsable
23
{
24
protected $reason;
25
26
public function __construct($reason)
27
{
28
$this->reason = $reason;
29
parent::__construct();
30
}
31
32
public function toResponse(RequestInterface $request) : ResponseInterface
33
{
34
return new JsonResponse([
35
'error' => $this->reason,
36
], 400);
37
}
38
}
Copied!
Last modified 2yr ago