Marooweb
Marooweb

Reputation: 466

How to Configure a Redirect without a custom Controller

i have two roles in my application : user and Admin i configure my access control that , the user cannot access to my page : hello , only the admin have this access: i used this code in my security.yml

access_control:
   - { path: ^/hello/, role: ROLE_ADMIN }

when a simple user will connect in the root : hello.I have the error : access denied:

how can i configure my app , that will redirect the simple user to the main page (or connection page) when he will want to access to the root hello.

Thank you for your help

Upvotes: 0

Views: 188

Answers (2)

Karol Wojciechowski
Karol Wojciechowski

Reputation: 422

Maybe you should create kernel.listener.accessDenied? Using Symfony2's AccessDeniedHandlerInterface

Upvotes: 1

Praveesh
Praveesh

Reputation: 1327

Within the controller that handles /hello route, add the logic to redirect the user based on the role. You may implement this by adding the following lines of code within that controller

    if ($this->get('security.context')->isGranted('ROLE_USER')) {
        return $this->redirect($this->generateUrl('Route for Main page'));
    }

Upvotes: 0

Related Questions