articus / path-handler
使用Mezzio进行API开发时,简化创建操作中间件并注册到路由器的库
0.8.3
2022-02-23 20:50 UTC
Requires
- php: ^7.1|^8.0
- laminas/laminas-servicemanager: ^3.4
- mezzio/mezzio: ^3.2
Requires (Dev)
- ext-json: *
- ext-uopz: *
- articus/data-transfer: ^0.5.2
- doctrine/annotations: ^1.8
- friends-of-phpspec/phpspec-code-coverage: *
- laminas/laminas-diactoros: ^2.3
- laminas/laminas-stdlib: ^3.2
- mockery/mockery: ^1.3
- nikic/fast-route: ^1.3
- phpspec/phpspec: ^5.1|^6.1|^7.0
- psr/simple-cache: ^1.0
Suggests
- articus/data-transfer: It is required for Articus\PathHandler\Attribute\Transfer and Articus\PathHandler\Producer\Transfer
- doctrine/annotations: It is required for Articus\PathHandler\MetadataProvider\Annotation
- laminas/laminas-stdlib: It is required for Articus\PathHandler\MetadataProvider\Annotation and Articus\PathHandler\MetadataProvider\PhpAttribute
- nikic/fast-route: It is required for Articus\PathHandler\Router\FastRoute
- psr/simple-cache: It is required for Articus\PathHandler\MetadataProvider\Annotation and Articus\PathHandler\MetadataProvider\PhpAttribute
This package is auto-updated.
Last update: 2024-08-24 22:32:38 UTC
README
此库通过减少每个API操作所需编写的样板代码量,显著简化了使用Mezzio进行API开发的过程。目的是提供一个更方便的方式来处理:
- 路由 - 自动注册所有操作的路由
- 消费 - 每个操作都可以根据其内容类型使用独特的算法解析请求体
- 属性(如PSR-7请求属性) - 每个操作都可以有其自己的请求属性集合,这些属性是从原始请求数据(如当前用户信息而不是认证头,经过验证的DTO而不是表单值数组,实体对象而不是带有其id的查询参数等)计算得出的
- 生产 - 每个操作都可以根据客户端接受的媒体类型使用独特的算法从操作结果准备响应体
因此,您可以专注于处理API操作,并减少在请求处理上编写辅助代码的时间。
如何安装?
只需将 "articus/path-handler"
添加到您的 composer.json,并查看库建议的额外依赖项,以使用您想要的可选组件。
如何使用?
首先,您需要一个包含Mezzio应用程序的项目。例如,您可以使用此安装程序生成一个。
接下来,您需要声明 处理器。每个处理器是一组可以在使用不同的HTTP方法访问API的某个 路径 时执行的所有 操作。任何类都可以是处理器,您只需要使用特殊的PHP属性对其进行装饰
namespace My; use Articus\PathHandler\PhpAttribute as PHA; use Articus\PathHandler\Exception; use Psr\Http\Message\ServerRequestInterface; #[PHA\Route('/entity')] //This is how you set path for handler operations class Handler { #[PHA\Post()] //This is how you declare HTTP method of the operation #[PHA\Consumer('application/json', 'Json')] //This is how you consume request body #[PHA\Attribute('Transfer', ['type'=>'My\DTO','object_attr'=>'dto','error_attr'=>'errors'])] //This is how you attribute request #[PHA\Producer('application/json', 'Json')] //This is how you produce response body from returned value public function handlePost(ServerRequestInterface $request): \My\DTO { $errors = $request->getAttribute('errors'); if (!empty($errors)) { //This is how you can return non-200 responses throw new Exception\UnprocessableEntity($errors); } /* @var \My\DTO $dto */ $dto = $request->getAttribute('dto'); return $dto; } }
最后,您需要为路由服务配置特殊工厂。以下是Laminas Service Manager的示例配置(示例使用YAML格式,以提高可读性)
dependencies: factories: Mezzio\Router\RouterInterface: Articus\PathHandler\RouteInjectionFactory Articus\PathHandler\MetadataProviderInterface: Articus\PathHandler\MetadataProvider\Factory\PhpAttribute Articus\PathHandler\Handler\PluginManager: [Articus\PluginManager\Factory\Laminas, Articus\PathHandler\Handler\PluginManager] Articus\PathHandler\Consumer\PluginManager: Articus\PathHandler\Consumer\Factory\PluginManager Articus\PathHandler\Attribute\PluginManager: Articus\PathHandler\Attribute\Factory\PluginManager Articus\PathHandler\Producer\PluginManager: Articus\PathHandler\Producer\Factory\PluginManager Articus\PathHandler\RouteInjectionFactory: paths: '': # List of your handlers - My\Handler # Configuration for handler plugin manager - sub-container dedicated for handlers Articus\PathHandler\Handler\PluginManager: factories: My\Handler: My\HandlerFactory
有关更多详细信息,请参阅文档。
祝您享受!
我希望这个库对我以外的某人也会有用。它用于生产目的,但缺乏许多改进,尤其是在测试和文档方面。
如果您有任何建议、意见、问题或修复,请随时提交问题或拉取请求。