Search code examples
phpsymfonyrolesymfony2-voter

Calling voters in twig without second object


I'm developing a new symfony project and I implemented and custom voter. In a controller I can call the voters through $this->isGranted('view',$team), but for this, I need a team-object. When I want to display a team, it is no problem. But now I have a base-template with only a top-menu. I only want to display the "Team"-Button, when $this->isGranted('view',$team) returns true, but in this template I never show any teams, so I don't have a team-object, which I can pass through this function. The first twig template is even a overview of the project. How can I do this? Should I pass a blank team-object to the base-template? Can anyone help me?


Solution

  • You should not rely on voter which is related to specific object. I suppose that teams can be shown by logged users (or something like this). So you should check if user is logged what you can do in Symfony by $this->isGranted('IS_AUTHENTICATED_FULLY') or base on the other state (new role) for example TEAM_VIEWER but remember that you have to add this role for these users.