I want to make a dynamic menu based on roles. Right now I am including the menu as a macro with a parameter(the role), but I am WRITING the role to test, and it works. I cannot get the value of the role from the controller at the template.
The thing is how to send the role of the user (already caught on the Controller) to the layout.
I have read a lot about the imposibility of creating global variables in twig, which could solve my problem.
This is my code:
# layout.html.twig #
{% block menu %}
{{ userRole('admin') }}
{% endblock %}
That is working but, as I said, I have to WRITE the role. I need to get the value.
I have also tried sending the value as a parameter, but the thing is in the controller I am rendering to the content, and it extends the layout.
public function loginAction(){
$ldaprdn = $_POST['login']; // ldap rdn or dn
$ldappass =$_POST['pass'];
$m = new Model();
$params = array('user' => $m->login($ldaprdn,$ldappass),);
$me = new Model();
$user = array('user' => $me->getSettings(),);
$this->render('::menu.html.twig',$user);
return $this->render('intranetBundle:Default:landinga.html.twig', $user);
}
It's the only thing I can't solve by large.
Any possible solution? Maybe the architecture design is not the best or something. Or perhaps I can use some tool that I don't know...
The big thing is to obtain the value of the role in any template, from PHP to TWIG. Once done, the problem is solved.