Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override logout action in symfony2

Tags:

php

symfony

I know logout action can be performed by symfony2 security controller by default. when we give the path Logout like this it works fine.

but I need to perform some action like storing some data into the database when logout is happened.So how can i achieve this thing.

If any have an idea please help me.

like image 963
raghava Avatar asked Jan 14 '23 06:01

raghava


1 Answers

you need to define new rule for logout action in routing.yml or annotation (it`s up to you)

logout_user:
    pattern:  /logoutUser
    defaults: { _controller: YourBundle:YourController:logout }

Then it`s only writing code for this action like this:

public function logoutAction() {
        //do whatever you want here 

        //clear the token, cancel session and redirect
        $this->get('security.context')->setToken(null);
        $this->get('request')->getSession()->invalidate();
        return $this->redirect($this->generateUrl('login'));
    }

There is also way to do the job proposed by tesmojones here symfony2 logout

like image 195
Tomasz Avatar answered Jan 16 '23 22:01

Tomasz