piotrgradzinski/uuid-extra-bundle

用于 Ramsey Uuid 的 Paramconverter、Normalizer 和 Form Type


README

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

Latest Stable Version License Build Status

免责声明

此存储库是 ekreative/uuid-extra-bundle 的分支,包含一些更新,因为原始项目已有一段时间未更新。

安装

Composer

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

AppKernel

将包包含到您的 AppKernel 中

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

配置

不需要配置

参数转换器

使用方式与其他参数转换器相同

/**
 * @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');
}

序列化器

就像正常化器应该做的那样

$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