定制 respect/validation-bundle 二次开发

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

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

respect/validation-bundle

Composer 安装命令:

composer require respect/validation-bundle

包简介

Symfony bundle for Respect\Validation

README 文档

README

Symfony bundle for Respect/Validation.

Installation

composer require respect/validation-bundle

Register the bundle in config/bundles.php:

return [
    // ...
    Respect\ValidationBundle\RespectValidationBundle::class => ['all' => true],
];

Usage

Inject Respect\Validation\ValidatorBuilder into any service via constructor injection:

use Respect\Validation\ValidatorBuilder;

final class MyService
{
    public function __construct(private readonly ValidatorBuilder $validator)
    {
    }

    public function process(mixed $value): void
    {
        $this->validator->stringType()->length(1, 100)->check(
            $value,
            new \DomainException('Invalid value.')
        );

        // proceed...
    }
}

ValidatorFactory is also available for autowiring if you need to build validators programmatically.

Custom rule namespaces

Add your own rule namespaces in config/packages/respect_validation.yaml:

respect_validation:
    rule_namespaces:
        - App\Validation\Rules

Rule classes under those namespaces (extending Respect\Validation\Validators\Core\Simple) become available via the builder — e.g. a class App\Validation\Rules\AcmeId can be used as $builder->acmeId()->isValid($value). User namespaces are checked before the built-in Respect\Validation\Validators, so custom rules can shadow defaults.

Container ownership

The bundle replaces Respect\Validation\ContainerRegistry's default container with Symfony's container, so v::email(), ValidatorBuilder::init(), and every other entry point in Respect/Validation resolves through the same container that serves your Symfony services. This happens at two points:

  1. Compile time — a compiler pass calls ContainerRegistry::setContainer() with the ContainerBuilder while the container is being built. Any compiler pass running after this one can call the static API safely.
  2. Runtime — the bundle overrides setContainer() so that the moment the kernel attaches the compiled container to bundles (before any boot() runs and before any service is resolved), the registry switches to the runtime container.

The practical guarantee: in any Symfony application code (controllers, console commands, listeners, service constructors), Respect\Validation\ContainerRegistry points at Symfony's container. There is no window in normal Symfony usage where the static API would resolve against Respect/Validation's default PHP-DI container.

The only situation this does not cover is code that runs Respect/Validation without a Symfony kernel at all — for example, a standalone script that uses v::email() directly. In that scenario there is no bundle to register, and Respect/Validation falls back to its built-in PHP-DI container, which is the expected behaviour outside of Symfony.

License

MIT. See LICENSE.

respect/validation-bundle 适用场景与选型建议

respect/validation-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 76 次下载、GitHub Stars 达 17, 最近一次更新时间为 2014 年 01 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「bundle」 「respect」 「validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 respect/validation-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 17
  • Watchers: 8
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-01-21