Middleware Overview

The Yee PHP Framework implements a version of the Rack protocol. As a result, a Yee application can have middleware that may inspect, analyze, or modify the application environment, request, and response before and/or after the Yee application is invoked.

Middleware Architecture

Think of a Yee application as the core of an onion. Each layer of the onion is middleware. When you invoke the Yee application’s execute() method, the outer-most middleware layer is invoked first. When ready, that middleware layer is responsible for optionally invoking the next middleware layer that it surrounds. This process steps deeper into the onion - through each middleware layer - until the core Yee application is invoked. This stepped process is possible because each middleware layer, and the Yee application itself, all implement a public call() method. When you add new middleware to a Yee application, the added middleware will become a new outer layer and surround the previous outer middleware layer (if available) or the Yee application itself.

Application Reference

The purpose of middleware is to inspect, analyze, or modify the application environment, request, and response before and/or after the Yee application is invoked. It is easy for each middleware to obtain references to the primary Yee application, its environment, its request, and its response:

<?php
class MyMiddleware extends \Yee\Middleware
{
    public function call()
    {
        //The Yee application
        $app = $this->app;

        //The Environment object
        $env = $app->environment;

        //The Request object
        $req = $app->request;

        //The Response object
        $res = $app->response;
    }
}

Changes made to the environment, request, and response objects will propagate immediately throughout the application and its other middleware layers. This is possible because every middleware layer is given a reference to the same Yee application object.

Next Middleware Reference

Each middleware layer also has a reference to the next inner middleware layer with $this->next. It is each middleware’s responsibility to optionally call the next middleware. Doing so will allow the Yee application to complete its full lifecycle. If a middleware layer chooses not to call the next inner middleware layer, further inner middleware and the Yee application itself will not be executed, and the application response will be returned to the HTTP client as is.

<?php
class MyMiddleware extends \Yee\Middleware
{
    public function call()
    {
        //Optionally call the next middleware
        $this->next->call();
    }
}