UIDs Controller Resolver
Symfony provides argument value resolvers to transform certain arguments of
controller methods into other objects. That's how you can get for example the
current request object just by typing any argument with the Request
class.
In Symfony 6.1 we're adding a new resolver to transform UID values into UID objects. For example, in previous Symfony versions you need to do this to get a UUID v4 from a controller argument:
1 2 3 4 5 6
#[Route(path: '/token/{token}')]
public function someControllerMethod(string $token): Response
{
$token = UuidV4::fromRfc4122($token);
// ...
}
In Symfony 6.1 you can do the same type-hinting the controller argument with the type of object you want to receive:
1 2 3 4 5 6 7
use Symfony\Component\Uid\UuidV4;
#[Route(path: '/token/{token}')]
public function someControllerMethod(UuidV4 $token): Response
{
// ...
}
Support for Mutation of Constructor Promoted Properties
Starting from PHP 8.0, you can use constructor property promotion to declare properties directly in the constructor signature. However, you can use PHPdoc annotations to change the type of the properties. In Symfony 6.1 we've improved the PropertyInfo component to take into account those PHPdoc modifications.
In the following example, the type of $someProperty
will be reported as
string
instead of mixed
:
1 2 3 4 5 6 7 8 9
class SomeClass
{
/**
* @param string $someProperty
*/
public function __construct(private mixed $someProperty)
{
}
}
Backed Enums Controller Resolver
Another controller argument resolver added in Symfony 6.1 allows to transform
arguments into backed enums cases. Take for example the well-known Suit
enum example:
1 2 3 4 5 6 7 8 9
namespace App\Model;
enum Suit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}
In Symfony 6.1 you can use Suit
as the type of the controller argument. If
the route parameter matches any of the enum values, Symfony will inject the
enum case. Otherwise, it will return a 404 HTTP error:
1 2 3 4 5 6 7 8
class CardController
{
#[Route('/cards/{suit}')]
public function list(Suit $suit): Response
{
// ...
}
}
PHPStan Pseudo-Types Support
Thanks to the popularity of static analyzers such as PHPStan, more and more developers use pseudo-types that describe arguments and return values with more precision.
For example, instead of string
you can use non-empty-string
; instead of
int
you can use positiveInt
; instead of int|float
you can use number
,
etc. In Symfony 6.1 we've improved the PropertyInfo to properly infer the right
PHP type of a variable described with these pseudo-types:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class SomeClass
{
/** @var negative-int */
public $property1;
/** @var non-empty-array */
public $property2;
/** @var non-empty-list */
public $property3;
/** @var non-empty-lowercase-string */
public $property4;
}
In Symfony 6.1, the PHP type of these properties will be correctly inferred as
integer
, array
, array
and string
respectively.
@Maxime good catch! It's fixed now. Thanks.