ekreative / uuid-extra-bundle

用于 Ramsey Uuid 的 Paramconverter、Normalizer 和 Form Type

安装次数: 158,739

依赖者: 0

建议者: 0

安全: 0

星标: 17

关注者: 5

分支: 12

公开问题: 11

类型:symfony-bundle

4.2.0 2022-01-17 13:28 UTC

README

一个方便的bundle,用于在项目中使用 ramsey/uuid

Latest Stable Version License Build Status

安装

Composer

php composer.phar require ekreative/uuid-extra-bundle

AppKernel

在您的 AppKernel 中包含此 bundle

public function registerBundles()
{
    $bundles = array(
        ...
        new Ekreative\UuidExtraBundle\EkreativeUuidExtraBundle()

配置

不需要配置

Param Converter

就像使用任何其他 param converter 一样使用

/**
 * @ParamConverter("uuid", class="Ramsey\Uuid\UuidInterface")
 * @Route("/simple/{uuid}")
 */
public function simpleAction(UuidInterface $uuid)
{
    return new Response($uuid->toString());
}

大多数情况下,只要在您的动作中使用类型提示,它就会自动工作

/**
 * @Route("/automatic/{uuid}")
 */
public function simpleAction(UuidInterface $uuid)
{
    return new Response($uuid->toString());
}

也适用于可选参数

/**
 * @Route("/optional/{uuid}")
 */
public function simpleAction(UuidInterface $uuid = null)
{
    return new Response($uuid ? $uuid->toString() : 'no uuid');
}

序列化器

就像一个正常的 normalizer 一样

$this->serializer->serialize($uuid, 'json')

结果为 "f13a5b20-9741-4b15-8120-138009d8e0c7"

反过来也是一样

$this->serializer->denormalize('"f13a5b20-9741-4b15-8120-138009d8e0c7"', UuidInterface::class, 'json')

结果为 $uuid

适用于您的对象等

表单类型

做您期望的一切

->add('uuid', UuidType:class)

如果您的模型有

/**
 * @Assert\Uuid
 */
private $uuid;

它将自动使用 UuidType