Stevan Tosic
Stevan Tosic

Reputation: 7199

Symfony 2 access_controll redirection with params

is there is some way to redirect to login page with message using symfony 2 access_control?

I need current role but only one exception in sub route. I want to create some session bag message without entering in controller.

Is that is possible?

access_control:
        - { path: /user/submission, role: [ROLE_USER_WITH_MESSAGE]}
        - { path: ^/user, role: [ROLE_USER] }

I asking this in reason to show message to user if he try to goes on certain route

Upvotes: 0

Views: 187

Answers (1)

Jan Rydrych
Jan Rydrych

Reputation: 2258

Yes, it's possible to se the session (of flashbag message) before login without controller involved. It can be done by an Authentication entry point service which is called right after start of authentication process. This Authentication entry point service is a part of firewall configuration as an entry_point key.

More on this can be found here: http://symfony.com/doc/current/components/security/firewall.html#entry-points

class MyAuthEntryPoint implements AuthenticationEntryPointInterface
{

    protected $router;

    public function __construct($router) {
        $this->router = $router;
    }

    public function start(Request $request, AuthenticationException $authException = null) {
        if ($request->get('_route') == 'submission') {
            $session = $request->getSession();
            $session->getFlashBag()->add('submissionUserMessage', 'Weclome user from submission!');

            //or if you do not want to use flashBag 
            $session->set('submissionUserMessage', 'Weclome user from submission!');
        }

        return new RedirectResponse($this->router->generate('login'));
    }
}

Service

Upvotes: 1

Related Questions