Viewed   125 times

I need to perform a set of actions after a user successfully logs in. This includes loading data from the database and storing it in the session.

What is the best approach to implementing this?



You can add a listener to the security.interactive_login event.

attach your listener like so. In this example I also pass the security context and session as dependencies.

Note: SecurityContext is deprecated as of Symfony 2.6. Please refer to

   # ...

   account.security_listener.class: CompanyAccountBundleListenerSecurityListener

   # ...

        class: %account.security_listener.class%
        arguments: ['@security.context', '@session']
            - { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }

and in your listener you can store whatever you want on the session. In this case I set the users timezone.


namespace CompanyAccountBundleListener;

use SymfonyComponentSecurityCoreSecurityContextInterface;
use SymfonyComponentHttpFoundationSessionSession;
use SymfonyComponentSecurityHttpEventInteractiveLoginEvent;

class SecurityListener

   public function __construct(SecurityContextInterface $security, Session $session)
      $this->security = $security;
      $this->session = $session;

   public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
        $timezone = $this->security->getToken()->getUser()->getTimezone();
        if (empty($timezone)) {
            $timezone = 'UTC';
        $this->session->set('timezone', $timezone);

Wednesday, November 16, 2022

First of all, move all your PHP code to the top. Without it, my code below wont work.

To do the redirect, use:


Also, please consider my advice. Since it's not the first question today and all your questions are related to basics, you should consider reading some good PHP book to understand how things work.

Here you can find useful links to free books: https://.com/tags/php/info

Wednesday, December 7, 2022

You need to give the array_shift() the parameter! Look this example:

$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack); // Here you give the parameter

You give the null parameter on array_shift() and you need to change it!


array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched. Read here for more

Wednesday, October 19, 2022

file_put_contents creates the file if it doesn't exist, but it fails if you try to put the file in a directory that doesn't exist. So you should try the following:

  • check if the images directory exists
  • check the write permissions of the directory
  • try with an absolute path, so in your case probably $target_dir = '/home/ragiththomas/Sites/asco-forum/images/';
Friday, December 9, 2022

You need to define a Spring Bean which implements ApplicationListener.

Then, in your code, do something like this:

public void onApplicationEvent(ApplicationEvent appEvent)
    if (appEvent instanceof AuthenticationSuccessEvent)
        AuthenticationSuccessEvent event = (AuthenticationSuccessEvent) appEvent;
        UserDetails userDetails = (UserDetails) event.getAuthentication().getPrincipal();

        // ....

Then, in your applicationContext.xml file, just define that bean and it will automatically start receiving events :)

Thursday, November 10, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :