PHP code example of thruster / http-middleware

1. Go to this page and download the library: Download thruster/http-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thruster / http-middleware example snippets




use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Thruster\Component\HttpMiddleware\Middlewares;

$middlewares = new Middlewares();

$middlewares->add(
    function (ServerRequestInterface $request, ResponseInterface $response, callable $next) {
        // ... Do something cool

        return $next($request, $response);
    }
);

$middlewares->add(new SomeCoolMiddleware());
$middlewares->add([$object, 'execute']);

$response = $middlewares($request, new Response());

$notUsefulMiddleware = [$object, 'execute'];
if ($middlewares->has($notUsefulMiddleware)) {
    $middlewares->remove($notUsefulMiddleware);
}

$response = $middlewares($request, new Response());



use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Thruster\Component\HttpMiddleware\MiddlewaresAwareTrait;

$application = new class
{
    use MiddlewaresAwareTrait;

    public function __construct()
    {
        $this->addMiddleware(
            function (ServerRequestInterface $request, ResponseInterface $response, callable $next) {
                // ... Do something cool

                return $next($request, $response);
            }
        );

        $this->addMiddleware(new SomeCoolMiddleware());
        $this->addMiddleware([$this, 'executeAction']);
    }

    public function executeAction(ServerRequestInterface $request, ResponseInterface $response, callable $next)
    {
        // ...
    }

    public function handleRequest(ServerRequestInterface $request)
    {
        $response = new Response();
        
        $response = $this->executeMiddlewares($request, $response);
    }
};


$application->handleRequest(ServerRequest::fromGlobals());



use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Thruster\Component\HttpMiddleware\RequestMiddlewares;

$middlewares = new RequestMiddlewares();

$middlewares->pre(
    function (ServerRequestInterface $request, ResponseInterface $response, callable $next) {
        // ... Do something cool

        return $next($request, $response);
    }
);

$middlewares->post(new SomeCoolMiddleware());

$response = $middlewares($request, new Response(), [$object, 'executeControllerAction']);



use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Thruster\Component\HttpMiddleware\RequestMiddlewaresAwareTrait;

$application = new class
{
    use RequestMiddlewaresAwareTrait;

    public function __construct()
    {
        $this->addPreMiddleware(
            function (ServerRequestInterface $request, ResponseInterface $response, callable $next) {
                // ... Do something cool

                return $next($request, $response);
            }
        );

        $this->addPostMiddleware(new SomeCoolMiddleware());
    }

    public function executeAction(ServerRequestInterface $request, ResponseInterface $response, callable $next)
    {
        // ...
    }

    public function handleRequest(ServerRequestInterface $request)
    {
        $response = new Response();

        $response = $this->executeMiddlewares($request, $response, [$this, 'executeAction']);
    }
};


$application->handleRequest(ServerRequest::fromGlobals());