PHP code example of ellipse / middleware-container

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

    

ellipse / middleware-container example snippets




namespace App;

use SomePsr11Container;

use Ellipse\Middleware\ContainerMiddleware;

// Get some Psr-11 container.
$container = new SomePsr11Container;

// Add a middleware in the container.
$container->set('some.middleware', function () {

    return new SomeMiddleware;

});

// Create a container middleware with the Psr-11 container and the entry id.
$middleware = new ContainerMiddleware($container, 'some.middleware');

// The middleware ->process() method retrieve the middleware from the container and proxy it.
$response = $middleware->process($request, new SomeRequestHandler);



namespace App;

use Psr\Http\Server\MiddlewareInterface;

use SomePsr11Container;

use Ellipse\Container\ReflectionContainer;
use Ellipse\Middleware\ContainerMiddleware;

// Get some Psr-11 container.
$container = new SomePsr11Container;

// Decorate the container with a reflection container.
// Specify the middleware implementations can be auto wired.
$reflection = new ReflectionContainer($container, [
    MiddlewareInterface::class,
]);

// Create a container middleware with the reflection container and a middleware class name.
$middleware = new ContainerMiddleware($reflection, SomeMiddleware::class);

// An instance of SomeMiddleware is built and its ->process() method is proxied.
$response = $middleware->process($request, new SomeRequestHandler);