Create Your Own

Tips and guide on how to create your own middleware

You can develop your own middleware by simply implementing the \Psr\Http\Server\MiddlewareInterface which requires you to implement the process() method. As per the PSR-15 spec, this method is called by the middleware dispatcher automatically to process the middleware when it's added to the middleware queue. The following is a basic example of how to implement the \Psr\Http\Server\MiddlewareInterface:

namespace Your\Namespace;

use \Psr\Http\Message\{ServerRequestInterface, ResponseInterface};
use \Psr\Http\Server\{RequestHandlerInterface, MiddlewareInterface};

class MiddlewareName implements MiddlewareInterface
{
    /* Process an incoming server request and return a response, 
     * optionally delegating response creation to a handler.  */
    public function process(
        ServerRequestInterface $request, 
        RequestHandlerInterface $handler
    ): ResponseInterface
    {
        // do something...
	
        // handle other requests
        $response = $handler->handle($request);

        // return the response
        return $response;
    }
}

That is the bare minimum required to create your own middleware. How it's instantiated depends entirely on your implementation. Typically, however, a middleware can be instantiated in one of the following ways when/if using BitFrame's default middleware dispatcher:

  1. Using the new keyword; for example new MiddlewareName();
  2. If the class constructor can be instantiated without requiring any arguments to be supplied to instantiate an object, then you could use the lazy instantiation method by simply providing the class name with its namespace, or by appending ::class to an object instance or class name.

There are of course a couple of other ways you could create a middleware as detailed out in the Add Middleware guide.

When starting out, you may find it useful to use an anonymous class as it allows you to quickly prototype classes before writing them formally. Since the syntax for anonymous classes is identical to declared classes, you can easily extract it to a named class later by simply cutting/pasting it into a file of its own.

Adding PSR-7 compatibility:

If you wish to implement PSR-7 compatibility in your middleware for any reason, you can do so simply by using the \BitFrame\Delegate\CallableMiddlewareTrait, for example:

namespace Your\Namespace;

use \Psr\Http\Message\{ServerRequestInterface, ResponseInterface};
use \Psr\Http\Server\{RequestHandlerInterface, MiddlewareInterface};

use \BitFrame\Delegate\CallableMiddlewareTrait;

class MiddlewareName implements MiddlewareInterface {
    use CallableMiddlewareTrait;

    // ...
}

This would delegate any PSR-7 style middleware calls to PSR-15 ones. This is of course, strictly optional, and may only be useful for supporting backward compatibility.

The default middleware dispatcher in BitFrame supports both PSR-15 and PSR-7 middlewares.

Keep in mind the reasons why PSR-15 style middleware signature was recommended (in place of the double-pass style used commonly with PSR-7).

Comments

Let us know if you have something to say or add