2016-04-24 71 views
0

登录页面所以,我使用FOSUserBundle和symfony的2.8项目重定向匿名用户/

,如果有任何的方式来重定向匿名用户/登录页面,如果他需要/页?

回答

0

首先,检查if the user is authorized。然后,使用if条件来重定向用户。索引的

完整的示例:

public function indexAction() 
{ 
    $context = $this->container->get('security.authorization_checker'); 
    if (!($context->isGranted('IS_AUTHENTICATED_REMEMBERED'))) { 
     $this->redirect('http://yourpage.com/login'); 
    } else { 
     //Do your other stuff here 
    } 
} 
1

使用防火墙来保护/

# app/config/security.yml 

security: 

    firewalls: 
     app: 
      pattern: ^/ 
      form_login: 
       provider: fos_userbundle 
       csrf_token_generator: security.csrf.token_manager 
      logout:  true 
      anonymous: true 

    access_control: 
     - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/, role: ROLE_ADMIN } 

更多细节在FOSUserBundle docs

+0

这就是我的security.yml现在的样子,但它不适用于我,我不知道为什么 –

+0

您是否遵循FOSUserBundle指南?激活捆绑等 – Jonny

+0

是的。一切工作完美,除了它 –