sidus/base-bundle

简化 Symfony 3 & 4 的路由和服务声明

安装次数: 37,985

依赖项: 14

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 5

开放问题: 3

类型:symfony-bundle

v1.1.4 2021-11-25 16:10 UTC

README

服务加载

如果您简单地继承自 SidusBaseExtension,则将加载此目录中所有现有的 YAML 文件:./Resources/config/services

<?php

namespace FooBarBundle\DependencyInjection;

use Sidus\BaseBundle\DependencyInjection\SidusBaseExtension;

class FooBarExtension extends SidusBaseExtension
{
}

路由

代替使用控制器,为每个动作声明服务,然后您只需声明路由如下

FooBarBundle\Action\MyAction: # Route name must match service id
    path: /path/{parameter}

您不需要声明 _controller 部分,它将与您的路由名称一起加载。

参数转换器

查看 AbstractParamConverter,声明参数转换器现在变得更简单了

编译器通过

GenericCompilerPass 允许您轻松地将带有特定标记的所有服务注入到另一个服务中。

实用工具

日期时间解析

DateTimeUtility::parse 将允许您轻松地从多种格式解析日期时间。

翻译

使用 TranslatorUtility 遍历各种翻译键,以便返回第一个匹配项。

验证器

您可以使用 BaseLoader 类从 PHP 数组加载约束集合,其语法与 Yaml 相同。

<?php
/** @var ValidatorInterface $validator */

use Sidus\BaseBundle\Validator\Mapping\Loader\BaseLoader;use Symfony\Component\Validator\Validator\ValidatorInterface;

$loader = new BaseLoader();

$constraints = $loader->loadCustomConstraints([
    ['Regex' => ['pattern' => '/^[a-z0-9]+(?:[-\_][a-z0-9]+)*$/']],
    ['NotNull' => null],
]);

foreach ($constraints as $constraint) {
    $violations = $validator->validate($data, $constraint);
    // Do stuff with the violations
}

表单

ChoiceTypeExtension 允许选择表单类型与可迭代对象一起工作。您不需要做任何事情。

任何表单类型现在都可用新选项:block_prefix 允许您直接为表单渲染选择自定义块前缀。

序列化器

有关更多支持,请参阅 SidusBaseSerializerBundle