Skip to content

Instantly share code, notes, and snippets.

@weierophinney
Created June 4, 2020 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weierophinney/7604e177b5255e6fad31188aa1c04cc8 to your computer and use it in GitHub Desktop.
Save weierophinney/7604e177b5255e6fad31188aa1c04cc8 to your computer and use it in GitHub Desktop.
Sample minimalist "mezzio" application (really just Stratigility + Mezzio routing)
{
"require": {
"laminas/laminas-stratigility": "^3.2",
"mezzio/mezzio-fastroute": "^3.0",
"laminas/laminas-diactoros": "^2.3",
"laminas/laminas-httphandlerrunner": "^1.2"
}
}
<?php
use FastRoute\RouteCollector;
use Laminas\Diactoros\ResponseFactory;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Laminas\HttpHandlerRunner\RequestHandlerRunner;
use Laminas\Stratigility\Middleware\ErrorHandler;
use Laminas\Stratigility\MiddlewarePipe;
use Mezzio\Router\FastRouteRouter;
use Mezzio\Router\Middleware\DispatchMiddleware;
use Mezzio\Router\Middleware\RouteMiddleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
require './vendor/autoload.php';
$responseFactory = new ResponseFactory();
$router = new FastRouteRouter();
$routes = new RouteCollector($router);
$app = new MiddlewarePipe();
$app->pipe(new ErrorHandler([$responseFactory, 'createResponse']));
$app->pipe(new RouteMiddleware($router));
$app->pipe(new DispatchMiddleware());
$routes->get('/hello', new class ($responseFactory) implements RequestHandlerInterface {
/** @var ResponseFactoryInterface */
private $responseFactory;
public function __construct(ResponseFactoryInterface $responseFactory)
{
$this->responseFactory = $responseFactory;
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$response = $this->responseFactory->createResponse();
$response->getBody()->write('Hello world');
return $response;
}
});
$runner = new RequestHandlerRunner(
$app,
new SapiEmitter(),
[ServerRequestFactory::class, 'fromGlobals'],
function (Throwable $error) use ($responseFactory): ResponseInterface {
$response = $responseFactory->createResponse(500)
->withHeader('Content-Type', 'application/json');
$response->getBody()->write(json_encode([
'error' => $error->getMessage(),
'trace' => $error->getTrace(),
]));
return $response;
}
);
$runner->run();
Copy link

ghost commented Nov 30, 2020

It would be very awesome if this gist would (still) work. Was looking for a light setup for testing without DI-Container and Mezzio Application classes.
Here is what i got:

Uncaught TypeError: Argument 1 passed to FastRoute\RouteCollector::__construct() must implement interface FastRoute\RouteParser, instance of Mezzio\Router\FastRouteRouter given, called in /home/bureau/Development/Tutorial/PHP/Laminas/Mezzio/Minimalist/MWOP/index.php on line 21 and defined in /home/bureau/Development/Tutorial/PHP/Laminas/Mezzio/Minimalist/MWOP/vendor/nikic/fast-route/src/RouteCollector.php:22

I'am still overlooking Laminas docs to resemble this gist. I know you are a busy person but maybe you find time to give another blueprint or pointing to the right direction for getting this to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment