gpt4 book ai didi

templates - 如何根据 symfony2 上登录的用户角色呈现动态菜单

转载 作者:行者123 更新时间:2023-12-01 23:59:23 24 4
gpt4 key购买 nike

根据用户角色在模板中生成动态菜单(该菜单将出现在所有其他应用的页面中)的最佳做法是什么?我在我的主模板中使用它:

{{render(controller('AcmeMainBundle:Sidebar:show'))}}  

这是 Controller

class SidebarController extends Controller {
public function showAction() {

$userRole = $this->getUser()->getRoles();
if (in_array("ROLE_ADMIN", $userRole)) {
return $this->render('AcmeMainBundle:Sidebar:sidebarAdmin.html.twig');
} else {
return $this->render('AcmeMainBundle:Sidebar:sidebarUser.html.twig');
}
}
}

但我觉得不好;你怎么看?谢谢!

最佳答案

您也可以在 View 级别实现此目的。在模板中,检查事件用户的角色并根据角色隐藏/显示菜单

{% if is_granted('ROLE_ADMIN') and not is_granted('ROLE_USER') %}

//Show admin stuff

{% else if is_granted('ROLE_USER') %}

//Show user stuff

{% endif %}

关于templates - 如何根据 symfony2 上登录的用户角色呈现动态菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22278749/

24 4 0