Skip to content

Conversation

@ayyoub-afwallah
Copy link
Contributor

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #43074
License MIT

Description

This PR introduces a new #[AsVoter] attribute that allows security voters to declare their priority directly within the class, as proposed in issue #43074.

Motivation

While #[AsTaggedItem] or #[Autoconfigure] can technically set voter priority, they require manually specifying tag names and options, making the configuration verbose, error-prone, and less intuitive.

The goal of #[AsVoter] is to simplify voter configuration and make it explicit, intuitive, and easy to use, following the pattern of other Symfony attributes such as #[AsEventListener].

The #[AsVoter] attribute provides:

  • Clearer intent: Explicitly designed for security voters
  • Better developer experience: More discoverable and easier to understand than generic tag configuration
  • Consistency: Aligns with other component-specific attributes like #[AsCommand], #[AsEventListener], #[AsMessageHandler], etc.
  • Foundation for future enhancements: Provides a dedicated attribute that can be extended with additional voter-specific configuration options in the future

Behavior

  • The attribute can be added once per class
  • The #[AsVoter] attribute overrides YAML configuration and other attributes ( #[AsTaggedItem] #[Autoconfigure])
  • This ensures the in-class configuration is always respected

Before

// config/services.yaml
services:
    App\Security\Voter\PostVoter:
        tags:
            - { name: security.voter, priority: 100 }
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;

#[AsTaggedItem('security.voter', ['priority' => 100])]
class PostVoter implements VoterInterface
{
    public function vote(TokenInterface $token, mixed $subject, array $attributes): int
    {
        // ...
    }
}

After

use Symfony\Component\Security\Core\Attribute\AsVoter;

#[AsVoter(priority: 100)]
class PostVoter implements VoterInterface
{
    public function vote(TokenInterface $token, mixed $subject, array $attributes): int
    {
        // ...
    }
}

@carsonbot carsonbot added this to the 7.4 milestone Nov 7, 2025
@ayyoub-afwallah ayyoub-afwallah force-pushed the feature/add-asVoter-attribute branch 2 times, most recently from 4fda52c to 0a3197a Compare November 9, 2025 10:16
@ayyoub-afwallah ayyoub-afwallah force-pushed the feature/add-asVoter-attribute branch from 0a3197a to a415e0d Compare November 9, 2025 10:33
@nicolas-grekas nicolas-grekas modified the milestones: 7.4, 8.1 Nov 16, 2025
@ayyoub-afwallah
Copy link
Contributor Author

@chalasr can I get your feedback on this one ? Thank you!

@stof
Copy link
Member

stof commented Dec 9, 2025

I'd rather make sure that the compiler pass registering voters is compatible with the AsTaggedIterm attribute instead of adding this new attribute (the voter tag itself is already autoconfigured based on the interface, and this new attribute causes 2 tags to be added as the existing tag autoconfigured for the interface will still be there)

@stof
Copy link
Member

stof commented Dec 9, 2025

And AsTaggedItem does not require specifying a tag name at all (it is not even able to specify one)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getDefaultPriority behavior not working with voter

4 participants