httpsoft / http-error-handler
处理 PSR-7 和 PSR-15 组件的错误
1.1.0
2023-05-05 21:14 UTC
Requires
- php: ^7.4|^8.0
- httpsoft/http-response: ^1.1
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- httpsoft/http-server-request: ^1.1
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^4.9|^5.2
README
此包实现了 Psr\Http\Server\MiddlewareInterface 和 Psr\Http\Server\RequestHandlerInterface。
文档
安装
此包需要 PHP 版本 7.4 或更高版本。
composer require httpsoft/http-error-handler
使用 ErrorHandler
use HttpSoft\ErrorHandler\ErrorHandler; /** * @var Psr\Http\Message\ServerRequestInterface $request * @var Psr\Http\Server\RequestHandlerInterface $handler * * @var HttpSoft\ErrorHandler\ErrorListenerInterface $logErrorListener * @var HttpSoft\ErrorHandler\ErrorListenerInterface $sendErrorListener * @var HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface $responseGenerator */ $errorHandler = new ErrorHandler($handler, $responseGenerator); $errorHandler->addListener($logErrorListener); $errorHandler->addListener($sendErrorListener); /** @var \Psr\Http\Message\ResponseInterface $response */ $response = $errorHandler->handle($request);
使用 ErrorHandlerMiddleware
use HttpSoft\ErrorHandler\ErrorHandlerMiddleware; /** * @var Psr\Http\Message\ServerRequestInterface $request * @var Psr\Http\Server\RequestHandlerInterface $handler * * @var HttpSoft\ErrorHandler\ErrorListenerInterface $logErrorListener * @var HttpSoft\ErrorHandler\ErrorListenerInterface $sendErrorListener * @var HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface $responseGenerator */ $errorHandler = new ErrorHandlerMiddleware($responseGenerator); $errorHandler->addListener($logErrorListener); $errorHandler->addListener($sendErrorListener); /** @var \Psr\Http\Message\ResponseInterface $response */ $response = $errorHandler->process($request, $handler);