New in Symfony 5.2: Controller argument attributes
October 8, 2020 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
PHP 8 will be released in a few weeks and it will include a game changer feature called attributes (or annotations). Symfony 5.2 already includes attributes to define routes and required dependencies, but we continued adding attributes support where it makes sense.
That's why in Symfony 5.2 you can also use PHP attributes for controller arguments.
Thanks to this new feature, we've introduced a #[CurrentUser]
attribute to
turn a controller argument into the object that represents the currently logged user:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// src/Controller/SomeController.php
namespace App\Controller;
use App\Entity\MyUser;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
class SomeController extends AbstractController
{
public function index(#[CurrentUser] MyUser $user)
{
// ...
}
}
In practice, this works by adding a new method to the ArgumentMetadata
object
passed to the argument value resolvers. If you define your own resolvers, you
can now use the getAttribute()
method, which returns the attribute that was
set on the argument (or null
if none was set).
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Thats nice but I would always inject
Symfony\Component\Security\Core\Security;
and get the user from there - $security->getUser().@Yoan yes, you can keep using the traditional way of injecting the current user. But you can use these attributes for other kind of arguments and even in your own resolvers, which gives you lots of possibilities!
I'm not sure to get what are such "lots of possibilities"... What can I do with these attributes that I can't already do with argument resolvers?
Is this in order to replace ParamConverter and other controller argument relative annotations from FrameworkExtra?
Currently, argument resolvers only tell you the basic properties of an argument: data type, default value, if it's nullable, if it's variadic, etc.
Thanks to attributes, you can also pass "configuration options" to those arguments. You could for example create the "#[Autowire]" attribute to define your own autowiring mechanism, or you could define "#[SomeAttribute(SomeClass::class, Some_CONSTANT, 24 * 3600)]" to evaluate expressions and constants and pass them to the resolvers.
@Massimiliano since attributes are a language feature, the processing of attributes are done by C in PHP interpreter, with little extra processing in user-defined space (PHP), so there might be a mix of benefits like performance gain, flexibility, and less code.
Note that this will turn the rest of the line into a comment in PHP7 so that you likely get a syntax error ;)
Won't attributes in PHP8 have the << >> syntax ? https://wiki.php.net/rfc/attributes_v2
@Stefan the syntax of attributes changed several times from the originally proposed one. This was the last voting to select the definitive syntax: https://wiki.php.net/rfc/shorter_attribute_syntax