PHP code example of codeinc / middleware-dispatcher

1. Go to this page and download the library: Download codeinc/middleware-dispatcher 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/ */

    

codeinc / middleware-dispatcher example snippets



use CodeInc\MiddlewareDispatcher\MiddlewareDispatcher;

// instantiating the dispatcher
$dispatcher = new MiddlewareDispatcher([
    new MyFirstMiddleware(),
    new MySecondMiddleware()
]);
$dispatcher->addMiddleware(new MyThirdMiddleware());

// handling the request 
// will return a NoResponseAvailable object if the request can not be processed by the middleware
// --> $psr7ServerRequest must be an object implementing ServerRequestInterface
$psr7Response = $dispatcher->handle($psr7ServerRequest); 


use CodeInc\MiddlewareDispatcher\MiddlewareIteratorDispatcher;

$dispatcher = new MiddlewareIteratorDispatcher(function():Generator {
    yield new MyFirstMiddleware();
    yield new MySecondMiddleware();
    yield new MyThirdMiddleware();
});
$psr7Response = $dispatcher->handle($psr7ServerRequest);