So, I'm using FOSUserBundle and symfony 2.8 project
if there is any way to redirect anonymous user to /login page, if he required / page?
So, I'm using FOSUserBundle and symfony 2.8 project
if there is any way to redirect anonymous user to /login page, if he required / page?
Use a firewall to protect /:
# 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 }
More details in the FOSUserBundle docs.
First, check if the user is authorized. Then, use an if condition to redirect the user.
Full example of your index:
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
}
}