简体   繁体   English

如何根据Symfony中的角色为两个控制器提供一个URL?

[英]How to have one URL for two controllers depending of roles in Symfony?

I'm trying to create my WebSite on Symfony 2.7. 我正在尝试在Symfony 2.7上创建我的WebSite。 I want to have two "home" with the basic url : "/". 我想要两个带有基本URL的“ home”:“ /”。 One for those who have the role ROLE_USER ( the connected ) and one for those who have IS_AUTHENTICATED_ANONYMOUSLY. 一个用于具有ROLE_USER(已连接)角色的用户,另一个用于具有IS_AUTHENTICATED_ANONYMOUSLY角色的用户。

I'm thinking about one URL that calls two controllers instead of calling the same controller that will show the first or second view depending of rights ( I think that it's not a good practice ). 我正在考虑一个URL调用两个控制器,而不是调用将根据权限显示第一个或第二个视图的同一个控制器(我认为这不是一个好习惯)。

Does someone know how to do it? 有人知道怎么做吗?

I don't even know if I'm going in the good practice so if you to lead me to another way, feel free, thanks. 我什至不知道我是否会采取良好的做法,因此,如果您引导我采用另一种方式,请放心,谢谢。

Best Regards, 最好的祝福,

This is the wrong way around, because Symfony will first try to match your route against a configured firewall , and then after doing the authentication will check the proper ROLE_USER authorization. 这是错误的方法,因为Symfony会首先尝试将您的路由与已配置的firewall匹配,然后在进行身份验证后将检查正确的ROLE_USER授权。

Because you want to allow IS_AUTHENTICATED_ANONYMOUSLY , you should check for ROLE_USER in your controller or template. 因为您想允许IS_AUTHENTICATED_ANONYMOUSLY ,所以应该在控制器或模板中检查ROLE_USER This is not something you can check based on the same URL. 您无法基于相同的URL进行检查。

In your controller: 在您的控制器中:

if ($this->get('security.authorization_checker')->isGranted('ROLE_USER')) {
    // ...
}

In your Twig template: 在您的Twig模板中:

{% if is_granted('ROLE_USER') %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM