定制 gdnacho/poob 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gdnacho/poob

Composer 安装命令:

composer require gdnacho/poob

包简介

Input/Output DTO generator, validator, and OpenAPI docs generator.

README 文档

README

Poob – Input/Output DTO generator, validator, and OpenAPI docs generator.

Quickly create Input DTOs, Output DTOs, and Field definitions using Symfony Validator. Input DTOs are automatically validated through a value resolver, as well as OpenAPI docs generation.

The goal is to provide a less opinionated micro-framework than API Platform, and a bundle like Nelmio/ApiDocBundle without the annotation boilerplate.

Features

  • Generate Input DTOs (poob:make:input-dto <name>)
  • Generate Output DTOs (poob:make:output-dto <name>)
  • Generate Field definitions (poob:make:field <name>)
  • Automatic request validation using a value resolver (RequestInputResolver)
  • Organizes generated classes under /src/Api:
    • /InputDto
    • /OutputDto
    • /Field
  • Generate API docs (poob:make:docs)

Installation

Add Poob to your Symfony project via Composer:

composer require gdnacho/poob

Then initialize (This will create the directories /src/Api and /config/packages/poob_api.yaml):

php bin/console poob:init

Usage

Controller

use App\Api\InputDto\UsernameInput;
use App\Api\OutputDto\UsernameOutput;
use App\Repository\UserRepository;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/api/users', name: 'app_user_get')]
public function getUser(
    UsernameInput $data,
    UserRepository $userRepository
): JsonResponse {
    // Find the user entity using the input DTO
    $user = $userRepository->findOneBy(['username' => $data->username]);

    // Return a response DTO, serialized to JSON automatically
    return $this->json(
        $user ? UsernameOutput::from($user) : ['error' => 'User not found']
    );
}
  • $data is automatically populated from the request and validated, using the UsernameInput DTO.
    • For GET requests, Poob reads the query parameters.
    • For POST, PUT, PATCH, or other request methods, Poob parses and validates the JSON request body.
  • If validation fails, the value resolver throws a ValidationException, which can be handled by an event listener.
  • The from() method from the Output DTO takes any object and serializes it. This example, thus, responds:
{
    "username": "string"
}

Optionally, you can also map requests directly to an unvalidated array if the parameter is named $requestData:

public function getUser(
    array $requestData,
    UserRepository $userRepository
): JsonResponse {
}

Input DTO

// src/Api/InputDto/UsernameInput.php
class UsernameInput
{
    #[Field\UsernameField]
    public $username;

    /**
     * This method runs after attribute validation and allows
     * implementing custom logic that depends on multiple fields, or mutate data as needed.
     */
    public function extra(): void
    {
    }
}

Field

// src/Api/Field/UsernameField.php
#[\Attribute]
class UsernameField extends Assert\Compound
{
    protected function getConstraints(array $options): array
    {
        return [
            new Assert\NotBlank(),
            new Assert\Type('string'),
            new Assert\Length(min: 2, max: 24),
            new Assert\Regex(
                pattern: '/^[A-Za-z0-9_]+$/',
                message: 'Username may only contain letters, numbers, and underscores.'
            ),
        ];
    }
}

Output DTO

// src/Api/OutputDto/UsernameOutput.php
class UsernameOutput extends OutputDto
{
    public function __construct(
        public $username,
    ) {
    }
}

Output DTO Helpers

All Output DTOs should extend OutputDto. This provides two convenient static methods:

  • from(object $source): static: Creates a new DTO from any object, such as entities.
  • collection(iterable $items): array: Converts a list of objects into an array of DTOs. Uses from() internally for each item.

API Docs generation

You may generate rudimentary OpenAPI documentation for your API with the poob:make:docs command. Poob will scan all your routes (With a prefix of /api by default) and Input DTOs schema and validation rules to generate it. $ref is not yet supported.

You can configure your API docs in config/packages/poob_api.yaml:

poob:
  docs:
    title: 'Poob API'
    version: '1.0.0'
    description: ''

    servers:
      - url: 'http://localhost:8000'
        description: 'Local'
      - url: 'https://api.example.com'
        description: 'Production'

    default_responses:
      '200':
        description: OK

    path_prefix: '/api'
    output: '%kernel.project_dir%/openapi.yaml'

Description & Summary attributes

You may use these attributes atop controllers for the API docs generation:

#[Route('/api/user/{id}', methods: ['GET'], name: 'app_user_get')]
#[Summary('Get user')] // Adds a summary
public function get(string $id, ListUserInput $data): JsonResponse

You can also use description for properties in InputDTOs:

class CreateUserInput extends InputDto
{
    #[Field\UsernameField]
    #[Description('Username must be 3-24 characters long')] // Adds a description
    public string $username;
}

Full CRUD controller example

final class UserController extends AbstractController
{
    public function __construct(
        private UserRepository $repo,
    ) {
    }

    #[Route('/api/user/{id}', methods: ['GET'], name: 'app_user_get')]
    #[Summary('Get user')]
    public function get(string $id, ListUserInput $data): JsonResponse
    {
        $user = $this->repo->find($id);
        if (!$user) {
            return $this->json(['error' => 'Not found'], 404);
        }

        return $this->json(ListUserOutput::from($user));
    }

    #[Route('/api/user', methods: ['GET'], name: 'app_user_list')]
    #[Summary('List user')]
    public function list(ListUserInput $data): JsonResponse
    {
        $users = $this->repo->findByFilters($data);

        return $this->json(ListUserOutput::collection($users));
    }

    #[Route('/api/user', methods: ['POST'], name: 'app_user_create')]
    #[Summary('Create user')]
    public function create(CreateUserInput $data, EntityManagerInterface $em): JsonResponse
    {
        $user = new User();
        $user->setUsername($data->username);
        $user->setAge($data->age);
        $user->setEmail($data->email ?? null);

        $em->persist($user);
        $em->flush();

        return $this->json(CreateUserOutput::from($user), 201);
    }

    #[Route('/api/user/{id}', methods: ['PATCH'], name: 'app_user_update')]
    #[Summary('Update user')]
    public function update(string $id, UpdateUserInput $data, EntityManagerInterface $em): JsonResponse
    {
        $user = $this->repo->find($id);
        if (!$user) {
            return $this->json(['error' => 'Not found'], 404);
        }

        if ($data->username !== null) $user->setUsername($data->username);
        if ($data->age !== null) $user->setAge($data->age);
        if ($data->email !== null) $user->setEmail($data->email);
        $em->flush();

        return $this->json(CreateUserOutput::from($user));
    }

    #[Route('/api/user/{id}', methods: ['DELETE'], name: 'app_user_delete')]
    #[Summary('Delete user')]
    public function delete(string $id, EntityManagerInterface $em): JsonResponse
    {
        $user = $this->repo->find($id);
        if (!$user) {
            return $this->json(['error' => 'Not found'], 404);
        }

        $em->remove($user);
        $em->flush();

        return $this->json('', 204);
    }
}

Contributing

Poob is just a small package with not a lot of thought put into it, mostly for me to use in my own projects. Regardless, contributions as small as just submitting issues are welcome.

"u gota get a groov!!!"

gdnacho/poob 适用场景与选型建议

gdnacho/poob 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 gdnacho/poob 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gdnacho/poob 我们能提供哪些服务?
定制开发 / 二次开发

基于 gdnacho/poob 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-30