How to use Symfony 4 Voters without denying access to a controller

656 views Asked by At

I’m using symfony 4 voters and love how it works to grant or deny permission to a controller method.

What I’m trying to achieve now is to check if user has permission to see a specific block in my twig view. I have a voter called Web:

I’d like to do {% if isGranted(‘Web’) %}{% endif %}

Is this possible? otherwise I’d like to get the result of my voter in a variable from the controller that ‘ill pass to the view without necessary denying access to the method/page.

Is this possible?

Thanks.

1

There are 1 answers

0
Mehdi Ben Sma On

I suggest you export your block into an another template then you include it in your original template with the render function

{{ render(controller('App\\Controller\\MyController::myRenderMethod')) }}

then in MyController you can do :


use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class MyController extends AbstractController
{
    public function myRenderMethod(Request $request)
    {
        if ($this->isGranted($attributes, $subject)) {
            //call your render method here
        }
    }
}