Asked  7 Months ago    Answers:  5   Viewed   49 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, March 31, 2021
answered 7 Months ago

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:

Wednesday, March 31, 2021
answered 7 Months ago

You need to put any strings starting with @ in quotes:

    ["@security.context", "@fos_user.user_manager"]

Reserved characters, like @, should be quoted. Unquoted @s were deprecated in Symfony 2.8. Symfony 3.0 will forbid you to use such definitions and will throw an exception.

If you don't have control over the bundle that registered the problematic configuration, send a pull request with a fix to the vendor. As a quick fix you can downgrade Symfony. 2.7 will work, as well as 2.8 (the later will only emit a deprecation notice).

Saturday, May 29, 2021
answered 5 Months ago

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 :)

Tuesday, June 22, 2021
answered 4 Months ago

I ran into the same issue and found the following solution in the documentation:

To run your functional tests, the WebTestCase class bootstraps the kernel of your application. In most cases, this happens automatically. However, if your kernel is in a non-standard directory, you'll need to modify your phpunit.xml.dist file to set the KERNEL_DIR environment variable to the directory of your kernel:

    <!-- ... -->
        <server name="KERNEL_DIR" value="/path/to/your/app/" />
    <!-- ... -->

So check your phpunit.xml.dist configuration file and try to add the absolute path to your app-directory.

Hope it helps.

Friday, July 30, 2021
Bálint Molnár
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :