PSR-15 Middleware support for Slim Framework v3
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.editorconfig
.gitignore
README.md
composer.json

README.md

PSR-15 Middleware support for Slim Framework v3

Installation

It's recommended that you use Composer.

$ composer require bnf/slim3-psr15 "^1.0"

Usage

Create an index.php file with the following contents:

<?php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;

require 'vendor/autoload.php';

$app = new \Slim\App();

$container = $app->getContainer();
/* Supply a custom callable resolver, which resolves PSR-15 middlewares. */
$container['callableResolver'] = function ($container) {
    return new \Bnf\Slim3Psr15\CallableResolver($container);
};

/* Add a PSR-15 middleware */
$app->add(new class implements Middleware {
    public function process(Request $request, RequestHandler $handler): Response
    {
        $request = $request->withAttribute('msg', 'Hello');
        return $handler->handle($request);
    }
});

$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
    $msg = $request->getAttribute('msg');
    $name = $args['name'];
    $response->getBody()->write("$msg, $name");

    return $response;
});
$app->run();

You may quickly test this using the built-in PHP server:

$ php -S localhost:8000

Going to http://localhost:8000/hello/world will now display "Hello, world".