deadmansswitch/responder
Composer 安装命令:
composer require deadmansswitch/responder
包简介
Responder component for Symfony applications
README 文档
README
Quick start to convert your traditional MVC symfony app into ADR app
Features
Typed responses from your actions
Instead of returning Response objects from your controllers, you can return DTO objects.
Before:
<?php declare(strict_types=1); namespace App\Controller; use App\DTO\CreateUserInput; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; #[Route('/users', name: 'create_user', methods: ['POST'])] final class UserController extends AbstractController { public function __invoke(#[MapRequestPayload] CreateUserInput $input): Response { $user = new User(); $user->setLogin($request->get('name')); $user->setEmail($request->get('email')); $user->setPassword($request->get('email')); // ... some logic, entity validation, password hashing, persisting etc. (delegate it to service in real app) return new JsonResponse(['id' => $user->getId()], Response::HTTP_CREATED); } }
After:
<?php declare(strict_types=1); namespace App\Controller; use App\DTO\CreateUserInput; use App\DTO\CompactUserDTO; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; #[Route('/users', name: 'create_user', methods: ['POST'])] final class CreateUser { public function __invoke(#[MapRequestPayload] CreateUserInput $input): CompactUserDTO { $user = new User(); $user->setLogin($input->login); $user->setEmail($input->email); $user->setPassword($input->password); // ... some logic, entity validation, password hashing, persisting etc. (delegate it to service in real app) return new CompactUserDTO( id: $user->getId(), login: $user->getLogin(), createdAt: $user->getCreatedAt(), ); } }
Benefits:
- Slightly lower amount of client code, no injection of serializer into each controller
- Business logic separated from response formating, so action becomes reusable
- Every action now gets a clear contract of object that it should return
- Ability to read actions using reflection and generate API documentation
Automatic response serialization
Using content-type negotiation, bundle will create proper instance of Responder to serialize your data for client purposes.
Just out ouf the box you've got JsonResponder that will serialize your DTOs into JSON responses.
You can create your own responders for different content type or override default JsonResponder with own implementation.
HTTP Response Codes
It uses default response codes for every HTTP Request Method, but you can easily override them on action level:
<?php declare(strict_types=1); namespace App\Domain\User\Action; #[HttpResponseCode(status: 202)] // <----------- 202 Accepted instead of 201 Created final class CreateUser { public function __invoke(CreateUserInput $input): CreateUserOutput { // ... } }
deadmansswitch/responder 适用场景与选型建议
deadmansswitch/responder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.99k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 08 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 deadmansswitch/responder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 deadmansswitch/responder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 21.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-26