Lumberjack
WebsiteRareloopTimber DocumentationTwig Documentation
v3
v3
  • Documentation
  • Getting Started
    • Installation
    • Quick Start
    • Configuration
  • The Basics
    • Lifecycle
    • Post Types
    • WordPress Controllers
    • Routing
    • HTTP Responses
  • Container
    • Using the Container
    • Service Providers
    • Facades
  • Misc
    • Changelog
    • Contributing
    • Notable Mentions
    • Code of Conduct
    • View on GitHub
    • Submit an issue
Powered by GitBook
On this page
  • Introduction
  • Available Responses
  1. The Basics

HTTP Responses

Introduction

All of your WordPress and Router controllers should return PSR7 compliant response. The most common use-case is rendering a twig view and passing in some context. Using Timber it would look like this:

use Timber\Timber;

return Timber::render('home', $context);

In Lumberjack, you should take advantage of the Rareloop\Lumberjack\Http\Responses\TimberResponse object to achieve the same thing:

use Rareloop\Lumberjack\Http\Responses\TimberResponse;

return new TimberResponse('home', $context);

The benefit to using these reponses is that they let you controll the HTTP status code & headers.

use Zend\Diactoros\Response\JsonResponse;

return new JsonResponse($data, 422, ['X-Total-Validation-Errors' => 2]);

Or if you prefer, you can use the withStatus and withHeader methods instead.

use Zend\Diactoros\Response\JsonResponse;

return (new JsonResponse($data, 422))
    ->withStatus(422)
    ->withHeader('X-Total-Validation-Errors', 2);

Available Responses

$response = new Rareloop\Lumberjack\Http\Responses\TimberResponse('home', $context);

// Check out their documentation for further details and examples:
// https://zendframework.github.io/zend-diactoros/
$response = new Zend\Diactoros\Response\TextResponse('Hello world!');
$response = new Zend\Diactoros\Response\HtmlResponse($htmlContent);
$response = new Zend\Diactoros\Response\XmlResponse($xml);
$response = new Zend\Diactoros\Response\JsonResponse($data);
$response = new Zend\Diactoros\Response\EmptyResponse(); // Basic 204 response:
$response = new Zend\Diactoros\Response\RedirectResponse('/user/login');
PreviousRoutingNextUsing the Container

Last updated 2 years ago

As we make use of a great 3rd party package: , Lumberjack includes an array of handy responses out-of-the-box.

Zend Diactoros