org_heigl / clacks-middleware
Implements the Clacks-Header as per http://www.gnuterrypratchett.com
Installs: 1 213
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 0
Open Issues: 2
Type:middleware
Requires
- php: ^7.1
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- guzzlehttp/psr7: ^1.2
- mockery/mockery: ^1.1
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-11-07 04:35:38 UTC
README
As described in http://www.gnuterrypratchett.com
Installation
composer require org_heigl/clacks-middleware
Usage
PSR-7
use Org_Heigl\Middleware\Clacks\Clacks; $clacks = new Clacks(); // add $clacks to your middleware-stack
When someone sends a request that already contains an X-Clacks-Overhead
header,
that header is returned.
And when you want to send a different name, you can set that on the constructor like this:
use Org_Heigl\Middleware\Clacks\Clacks; $clacks = new Clacks('Jane Doe'); // add $clacks to your middleware-stack
PSR-15
If you are using a PSR15 based stack you can use the ClacksMiddleware
-Class instead like this:
use Org_Heigl\Middleware\Clacks\ClacksMiddleware; $clacks = new ClacksMiddleware(); // Add $clacks to your middleware-stack
Adding $clacks to the middleware
Slim 4
use Slim\Factory\AppFactory; use Org_Heigl\Middleware\Clacks\ClacksMiddleware; $app = AppFactory::create(); $app->add(new ClacksMiddleware()); $app->run();
Slim 3
use \Slim\App; use Org_Heigl\Middleware\Clacks\Clacks; $app = new App(); $app->add(new Clacks()); $app->get('/', function ($request, $response, $args) { $response->getBody()->write(' Hello '); return $response; }); $app->run();
Mezzio
use Laminas\Diactoros\ResponseFactory; use Laminas\Diactoros\ServerRequestFactory; use Laminas\HttpHandlerRunner\Emitter\SapiEmitter; use Laminas\HttpHandlerRunner\RequestHandlerRunner; use Laminas\Stratigility\MiddlewarePipe; use Org_Heigl\Middleware\Clacks\ClacksMiddleware; use function Laminas\Stratigility\middleware; $app = new MiddlewarePipe(); // Landing page $app->pipe(middleware(new ClacksMiddleware())); $server = new RequestHandlerRunner( $app, new SapiEmitter(), static function () { return ServerRequestFactory::fromGlobals(); }, static function (\Throwable $e) { $response = (new ResponseFactory())->createResponse(500); $response->getBody()->write(sprintf( 'An error occurred: %s', $e->getMessage )); return $response; } ); $server->run();