los / api-problem
PHP 中间件,在发生错误时返回格式化的 JSON 响应
2.0.0
2017-03-20 15:54 UTC
Requires
- php: ^7.0 || ^7.1
- psr/http-message: ~1.0
- zendframework/zend-diactoros: ^1.3
- zendframework/zend-stratigility: ^2.0.1
Requires (Dev)
- container-interop/container-interop: ^1.2.0
- phpunit/phpunit: ^5.0 || ^6.0
- satooshi/php-coveralls: ~0.6
- squizlabs/php_codesniffer: ^2.3
- zendframework/zend-expressive-tooling: ^0.3.2
This package is auto-updated.
Last update: 2024-08-29 04:14:09 UTC
README
此中间件在发生错误时返回格式化的 JSON。它受到了 ApiProblem 库的启发,但依赖项更少。
用法
只需将中间件作为应用程序中的最后一个添加。
例如
$app->pipe(new \LosMiddleware\ApiProblem\ApiProblem());
它将返回
{
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title": "Method Not Allowed",
"status": 405,
"detail": "error",
"code": "14868ef1-7ef4-4feb-a7ae-9a12c9097375"
}
Zend Expressive
如果您使用 expressive-skeleton,可以将 config/los-api-problem.global.php.dist
复制到 config/autoload/los-api-problem.global.php
。
它使用 Expressive 的 FinalHandler 功能。如果您希望使用其他 FinalHandler,可以手动添加此中间件
return [ 'dependencies' => [ 'invokables' => [ LosMiddleware\ApiProblem\ApiProblem::class => LosMiddleware\ApiProblem\ApiProblem::class, ], ], 'middleware_pipeline' => [ 'error' => [ 'middleware' => [ LosMiddleware\ApiProblem\ApiProblem::class, ] 'error' => true, ], ], ];
但 404 错误将由 FinalHandler 处理,而不是此中间件。