composer.json
· 214 B · JSON
Raw
{
"require": {
"laminas/laminas-stratigility": "^3.2",
"mezzio/mezzio-fastroute": "^3.0",
"laminas/laminas-diactoros": "^2.3",
"laminas/laminas-httphandlerrunner": "^1.2"
}
}
1 | { |
2 | "require": { |
3 | "laminas/laminas-stratigility": "^3.2", |
4 | "mezzio/mezzio-fastroute": "^3.0", |
5 | "laminas/laminas-diactoros": "^2.3", |
6 | "laminas/laminas-httphandlerrunner": "^1.2" |
7 | } |
8 | } |
index.php
· 2.0 KiB · PHP
Raw
<?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();
1 | <?php |
2 | |
3 | use FastRoute\RouteCollector; |
4 | use Laminas\Diactoros\ResponseFactory; |
5 | use Laminas\Diactoros\ServerRequestFactory; |
6 | use Laminas\HttpHandlerRunner\Emitter\SapiEmitter; |
7 | use Laminas\HttpHandlerRunner\RequestHandlerRunner; |
8 | use Laminas\Stratigility\Middleware\ErrorHandler; |
9 | use Laminas\Stratigility\MiddlewarePipe; |
10 | use Mezzio\Router\FastRouteRouter; |
11 | use Mezzio\Router\Middleware\DispatchMiddleware; |
12 | use Mezzio\Router\Middleware\RouteMiddleware; |
13 | use Psr\Http\Message\ResponseFactoryInterface; |
14 | use Psr\Http\Message\ResponseInterface; |
15 | use Psr\Http\Message\ServerRequestInterface; |
16 | use Psr\Http\Server\RequestHandlerInterface; |
17 | |
18 | require './vendor/autoload.php'; |
19 | |
20 | $responseFactory = new ResponseFactory(); |
21 | $router = new FastRouteRouter(); |
22 | $routes = new RouteCollector($router); |
23 | $app = new MiddlewarePipe(); |
24 | |
25 | $app->pipe(new ErrorHandler([$responseFactory, 'createResponse'])); |
26 | $app->pipe(new RouteMiddleware($router)); |
27 | $app->pipe(new DispatchMiddleware()); |
28 | |
29 | $routes->get('/hello', new class ($responseFactory) implements RequestHandlerInterface { |
30 | /** @var ResponseFactoryInterface */ |
31 | private $responseFactory; |
32 | |
33 | public function __construct(ResponseFactoryInterface $responseFactory) |
34 | { |
35 | $this->responseFactory = $responseFactory; |
36 | } |
37 | |
38 | public function handle(ServerRequestInterface $request): ResponseInterface |
39 | { |
40 | $response = $this->responseFactory->createResponse(); |
41 | $response->getBody()->write('Hello world'); |
42 | return $response; |
43 | } |
44 | }); |
45 | |
46 | $runner = new RequestHandlerRunner( |
47 | $app, |
48 | new SapiEmitter(), |
49 | [ServerRequestFactory::class, 'fromGlobals'], |
50 | function (Throwable $error) use ($responseFactory): ResponseInterface { |
51 | $response = $responseFactory->createResponse(500) |
52 | ->withHeader('Content-Type', 'application/json'); |
53 | $response->getBody()->write(json_encode([ |
54 | 'error' => $error->getMessage(), |
55 | 'trace' => $error->getTrace(), |
56 | ])); |
57 | return $response; |
58 | } |
59 | ); |
60 | $runner->run(); |