Home » Php » Looking for a can_access_route method

Looking for a can_access_route method

Posted by: admin October 29, 2018 Leave a comment

Questions:

The security of my Symfony 4 application is using @security annotations in controllers :

/**
 * @Route("/cat/list", name="cat_list")
 *
 * @Security("is_granted('ROLE_XYZ'")
 */
public function listAction()
{
    // [...]
}

I am building a menu with twig from a list of route names :

{% for route_name in ["cat_list","cat_map", ,"cat_trips"] %}
    <a href="{{ path(route_name) }}"/> {{ route_name|trans }} </a>
{% endfor %}

I would like to add a security check to only display the routes my user have access, something like that :

{% for route_name in ["cat_list","cat_map", ,"cat_trips"] %}
    {% if can_access_route(route_name) %}
        <a href="{{ path(route_name) }}"/> {{ route_name|trans }} </a>
    {% endif %}
{% endfor %}

Is there something built in Symfony for that ? Or how would you build is_route_granted() ?

Answers: