New in Symfony 5.3: UID Improvements

The UID Component was introduced in Symfony 5.1 as an experimental feature to help you generate and work with UIDs (universally unique identifiers) such as UUIDs and ULIDs. In Symfony 5.3 this component is no longer considered experimental and we added some new features to it.

New Ways to Generate UIDs¶

        Contributed by 
        Thomas Calvet 
        in #39507
        and #39850.

Creating UID values is easy thanks to methods like Uuid::v1(), Uuid::v4(), new Ulid(), etc. In Symfony 5.3 we’ve added new methods so you can create UID objects from several string representations of UIDs: 1 2 3 4 5 6 7 8 9$uuid = Uuid::fromBinary("\xd9\xe7\xa1\x84\x5d\x5b\x11\xea\xa6\x2a\x34\x99\x71\x00\x62\xd0"); $uuid = Uuid::fromBase32('6SWYGR8QAV27NACAHMK5RG0RPG'); $uuid = Uuid::fromBase58('TuetYWNHhmuSQ3xPoVLv9M'); $uuid = Uuid::fromRfc4122('d9e7a184-5d5b-11ea-a62a-3499710062d0');

$ulid = Ulid::fromBinary("\x01\x71\x06\x9d\x59\x3d\x97\xd3\x8b\x3e\x23\xd0\x6d\xe5\xb3\x08"); $ulid = Ulid::fromBase32('01E439TP9XJZ9RPFH3T1PYBCR8'); $ulid = Ulid::fromBase58('1BKocMc5BnrVcuq2ti4Eqm'); $ulid = Ulid::fromRfc4122('0171069d-593d-97d3-8b3e-23d06de5b308');

In addition, we’ve added several services such as ulid.factory and uuid.factory (and their corresponding autowiring aliases UlidFactory, UuidFactory, etc.) so you can customize the creation of UIDs. The defaults of the generated UIDs are now configured semantically: 1 2 3 4# config/packages/framework.yaml framework: uid: default_uuid_version: 4

Finally, we added UlidGenerator and UuidGenerator classes so you can use them as Doctrine ID generators, to use UIDs as the value of your primary keys in Doctrine entities.

UID Form Types¶

        Contributed by 
        Gemorroj 
        in #39863.

The integration with the Form component was one of the last missing features of the UIDs. That’s why in Symfony 5.3 we’ve added the UuidType and UlidType form types so you can work with UID values in your forms. These form types render a <input type="text"> field with the string representation of the given UID object and transform it back to a UID object when submitting the form.

Serializing UIDs¶

        Contributed by 
        Thomas Calvet 
        in #39675.

The Serializer component includes a UidNormalizer to transform objects of type Symfony\Component\Uid\AbstractUid into strings. In Symfony 5.3 we’ve improved it to support different string formats. You can now pass the UidNormalizer::NORMALIZATION_FORMAT_KEY context option to the serializer and set it to the desired format (e.g. UidNormalizer::NORMALIZATION_FORMAT_RFC_4122, etc.)

Generate and Inspect UIDs in the Console¶

        Contributed by 
        Thomas Calvet 
        in #39883.

The UID component now includes some console commands to help you generate and inspect the details of UUID/ULID values. For example: 1 2 3 4 5 6$ php bin/console uuid:generate --random-based $ php bin/console ulid:generate --count=2 --format=rfc4122

run them with the --help option to check all their options:

$ php bin/console uuid:generate --help $ php bin/console ulid:generate --help

1 2 3 4 5 6 7 8 9 10$ php bin/console ulid:inspect 01F2TTCSYK1PDRH73Z41BN1C4X


Label Value


Canonical (Base 32) 01F2TTCSYK1PDRH73Z41BN1C4X Base 58 1BYGm16jS4kX3VYCysKKq6 RFC 4122 0178b5a6-67d3-0d9b-889c-7f205750b09d


Timestamp 2021-04-09 08:01:24.947


                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/2VmBv_iYqHE/new-in-symfony-5-3-uid-improvements

Created 4y | May 4, 2021, 8:20:12 AM


Login to add comment

Other posts in this group

New Core Team Members, 2025 Edition

A few weeks ago, I had the pleasure of announcing the formation of the Symfony UX Core Team, a dedicated group working to enhance the frontend development experience within the Symfony ecosystem. Toda

Feb 24, 2025, 4:20:03 PM | Symfony
SymfonyLive Paris 2025 : Du lego de composants pour un bundle Gotenberg !

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

Feb 24, 2025, 1:50:07 PM | Symfony
A Week of Symfony #947 (17-23 February 2025)

This week, development activity focused on new security features. The upcoming Symfony 7.3 version added support for security voters to explain their vote, improved the IsGranted attribute to allow us

Feb 23, 2025, 10:10:09 AM | Symfony
SymfonyLive Berlin 2025: Agentic Applications with Symfony

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

Feb 21, 2025, 9:20:12 AM | Symfony
SymfonyLive Paris 2025 : Postgres pour vos besoins NoSQL

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

Feb 20, 2025, 10:10:13 AM | Symfony
SymfonyLive Berlin 2025: Asynchronous PHP

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

Feb 19, 2025, 8:40:05 AM | Symfony
SymfonyLive Paris 2025 :  Le Composant Symfony Mapper

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

Feb 18, 2025, 2:10:24 PM | Symfony