PHP code example of schnittstabil / psr7-middleware-stack

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

    

schnittstabil / psr7-middleware-stack example snippets



use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Schnittstabil\Psr7\MiddlewareStack\MiddlewareStack;

$newMiddleware = (new MiddlewareStack())
  ->add($someMiddleware4)
  ->add($someMiddleware3)
  ->add(
    function (RequestInterface $req, ResponseInterface $res, callable $next) {
      $res->getBody()->write('Greetings from the the 2nd middleware.');
      return $next($req, $res);
    }
  )
  ->add($someMiddleware1);


Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr7Middlewares\Middleware;
use Slim\App;
use Slim\Http\Stream;
use Schnittstabil\Psr7\MiddlewareStack\MiddlewareStack;

// setup oscarotero/psr7-middlewares
Middleware::setStreamFactory(function ($file, $mode) {
    return new Stream(fopen($file, $mode));
});

$app = new App();

$app->getContainer()['minifyMiddleware'] = function ($c) {
    return (new MiddlewareStack())
        ->add(Middleware::Minify())
        ->add(Middleware::FormatNegotiator());
};

$app->get('/', function (RequestInterface $request, ResponseInterface $response) {
    $body = '<!-- comment --><h1>Hello world!</h1><!-- comment -->';

    return $response->write($body);
})->add('minifyMiddleware');

$app->run();