StackPHP – HttpKernelInterface based middlewares

onion

The HttpKernelInterface models web request and response as PHP objects, giving them value semantics. Stack is a convention for composing HttpKernelInterface middlewares. By wrapping your application in decorators you can add new behaviour from the outside.

In the screenshot above Session is a decorator wrapped around Authentication which itself is a decorator wrapped around the original $app. The incoming $request is passed through these three layers, one after the other.

The key part of StackPHP is \Symfony\Component\HttpKernel\HttpKernelInterface: each layer must implement it.

<?php namespace Cartisan\Middleware;

use Cartisan\Core\App;  
use Symfony\Component\HttpFoundation\Request;  
use Symfony\Component\HttpKernel\HttpKernelInterface;

class Logger implements HttpKernelInterface  
{
    protected $app;

    public function __construct(HttpKernelInterface $app)
    {
        $this->app = $app;
    }

    public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
    {

        // Add logic here to be placed in the request response cycle
        // ...

        // Call handle on inner layer
        return $this->app->handle($request);

    }
}

Stack — HttpKernelInterface based middlewares →
StackPHP Middleware →

(via)

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.