Middlewares

In PHP, a middleware is a piece of code that operates between the incoming HTTP request and the corresponding application logic. It allows developers to perform actions before or after the request reaches the controller.

Build a middlewares

You can very easily create you own middlewares.

You could either just duplicate the template file "app/Http/Middlewares/HelloWorld.php", or just create a new one and copy over the template content bellow.

<?php

namespace Http\Middlewares;

use MaplePHP\Handler\Interfaces\MiddlewareInterface;
use MaplePHP\Http\Interfaces\ResponseInterface;
use MaplePHP\Http\Interfaces\RequestInterface;
use MaplePHP\Foundation\Http\Provider;

class HelloWorld implements MiddlewareInterface
{
    private $provider;

    public function __construct(Provider $provider)
    {
        $this->provider = $provider;
    }

    /**
     * Will load before the controllers
     * @param  ResponseInterface $response
     * @param  RequestInterface  $request
     * @return ResponseInterface|void
     */
    public function before(ResponseInterface $response, RequestInterface $request)
    {
    }

    /**
     * Custom Before middleware (Will load before the controllers)
     * @param  ResponseInterface $response
     * @param  RequestInterface  $request
     * @return ResponseInterface|void
     */
    public function yourCustomMethod(ResponseInterface $response, RequestInterface $request)
    {
    }

    /**
     * Will load after the controllers
     * @param  ResponseInterface $response
     * @param  RequestInterface  $request
     * @return ResponseInterface|void
     */
    public function after(ResponseInterface $response, RequestInterface $request)
    {
    }
}

MaplePHP middlewares

MaplePHP comes with a set of middlewares out of the box and this list will grow in time:

Document

You can use the Document middleware to add head, navigation and footer partial.

[Http\Middlewares\Document::class, ["after" => ["head", "navigation", "footer"]]],

Meta

Will set up your HTML document's Meta data (title, descriptions), which you can easily modify in your controllers. It will also load in Responder capabilities that add the possibility to communicate seamlessly with the frontend code.

MaplePHP\Foundation\Dom\Middleware\Meta::class

Last modify

Will set up last modify cache functionality.

MaplePHP\Foundation\Cache\Middleware\LastModified::class

Session start

Will open up a session.

MaplePHP\Foundation\Auth\Middleware\SessionStart::class

Logged out

Will check if is public zone (logged out).

[MaplePHP\Foundation\Auth\Middleware\LoggedIn::class, "publicZone"]

Logged in

Will check if is private zone (logged in).

[MaplePHP\Foundation\Auth\Middleware\LoggedIn::class, "privateZone"]

More will come in the future!

Now you can use the middleware just as a controller with the exception for that a middleware has before and after method with the possibility to also add/bind a custom method to the before call.

Last updated