New in Symfony 6.3: Enum Improvements

Symfony 5.4 introduced support for Enums in forms November 2021. Later, in
Symfony 6.2, we improved enums support to allow using them in YAML files,
env vars, etc. In Symfony 6.3 we’re improving enums support again.

Enums in Expressions

Contributed by
Alexandre Daubois

in #48669.

The ExpressionLanguage component now includes a function called enum()
to get the case of an enumeration inside expressions:

namespace AppConfigOrder;

enum OrderStatus
{
case Paid = ‚paid‘;
case Pending = ‚pending‘;
case Refunded = ‚refunded‘;
case Deleted = ‚deleted‘;
}

// inside expressions, namespace separators must be escaped with 3 backslashes
$expressionLanguage->evaluate(‚order.status == enum(„App\Config\OrderStatus::Paid“)‘)

Enums Support in EnumNode Config

Contributed by
Thomas Calvet

in #49098.

When using the Config component, you can now use enum values as part of the
allowed values of enum nodes defined with the enumNode() method:

enum OrderDelivery: string
{
case Standard = ’standard‘;
case Expedited = ‚expedited‘;
case Priority = ‚priority‘;
}

$rootNode
->children()
->enumNode(‚delivery‘)
// You can provide all values of the enum…
->values(OrderDelivery::cases())

// … or you can pass only some values next to other scalar values
->values([OrderDelivery::Priority, OrderDelivery::Standard, ‚other‘, false])
->end()
->end()
;

Enums as Route Parameters

Contributed by
Nadim AL ABDOU

in #50031.

In Symfony 6.3, you can use the value of backed enums as route arguments and let
Symfony transform them automatically into their associated enum case:

// src/Controller/OrderController.php
namespace AppController;

use AppConfigOrderStatus;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;

class OrderController extends AbstractController
{
#[Route(‚/orders/list/{status}‘, name: ‚list_orders_by_status‘)]
public function list(OrderStatusEnum $status = OrderStatus::Paid): Response
{
// …
}
}

An incoming request to e.g. /orders/list/deleted will make the $status
controller argument to be the case OrderStatus::Deleted.

Contributed by
Nicolas Philippe

in #48820.

Related to this, we’ve also improved the parameter bag used to get values from
the Request to add a getEnum() method. This converts e.g. the values from a
query string into backed enum cases:

// src/Controller/OrderController.php
namespace AppController;

// …
use SymfonyComponentHttpFoundationRequest;

class OrderController extends AbstractController
{
#[Route(‚/orders/list/‘, name: ‚list_orders‘)]
public function list(Request $request): Response
{
// the second argument is the FQCN of the backed enum to use when transforming the value
$status = $request->query->getEnum(’status‘, OrderStatus::class);

// …
}
}

Sponsor the Symfony project.

Symfony Blog

Read More

Latest News

PHP-Releases

PHP 8.3.8 released!

PHP 8.2.20 released!

PHP 8.1.29 released!

Generated by Feedzy