Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:21:01+00:00 2026-06-17T12:21:01+00:00

We want to redirect to a path or another after login. Also we need

  • 0

We want to redirect to a path or another after login. Also we need to pass de language in this path.

We have a Login Listener as a service to check this. Our problem is to do the redirect accion.
Code:

<service id="acme.login_listener" class="acme\DemoBundle\Listener\LoginListener">
   <tag name="kernel.event_listener" event="security.interactive_login" method="onSecurityInteractiveLogin" priority="-1" />
   <tag name="kernel.event_listener" event="filter.response" method="onKernelResponse" />            
   <argument type="service" id="service_container" />  
   <argument type="service" id="router" />          
</service>

The listener code:

namespace Acme\DemoBundle\Listener;

use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;

class LoginListener 
{
    protected $container;
    protected $router;

    public function __construct(ContainerInterface $container, Router $router)
    {
        $this->container = $container;
        $this->router = $router;
    }

    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {                
        $token = $event->getAuthenticationToken();
        $request = $event->getRequest();

        $username = $token->getUsername();

        $em = $this->container->get('doctrine')->getEntityManager();

        $user = $em->getRepository('acmeDemoBundle:User')->findOneByUsername($username);


        $locale = strtolower($user->getUsuari()->getIdioma()->getCodi6391());
        $locale = 'ca';  //force locale test

        $session = $request->getSession();
        $session->setLocale($locale);          
        $session->set('idioma', $locale);    
        $session->set('_locale', $locale); 
        $session->set('locale', $locale);

        $url = $this->router->generate('onePath', array('_locale' => $locale));

        //$event->setResponse(new RedirectResponse($url));
        //$event->stopPropagation();
        //echo $url;exit;

        $response = new RedirectResponse($url);            
        return $response;  
...

Our problem is to do the redirect to the wished path in onSecurityInteractiveLogin function.

We tried to do the $event->setResponse but it doesn’t work. This $event var doesn’t have this method.

Any idea?
Thanks for your help.


Edited: second option (Jan 27, 2012)

Following: http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/

I tried to use Handlers. I configured the services.xml:

<service id="company.login_success_handler" class="project\demoBundle\Handler\LoginSucessHandler">
    <tag name="monolog.logger" channel="project" />
    <argument type="service" id="logger" /> 
    <argument type="service" id="router" />          
</service>

ON security.yml:

firewalls:
    main:
        pattern: /.*
        form_login:
            check_path: /login_check
            login_path: /login
            success_handler: company.login_success_handler
            use_forward: false

And in Symfony/src/project/demoBundle/Handler/loginSuccessHandler.php

<?php 

namespace project\demoBundle\Handler; 

use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; 
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 
use Symfony\Component\Security\Core\SecurityContext; 

use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\Routing\Router; 

class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface 
{ 

  protected $router; 
  protected $security; 

  public function __construct(Router $router, SecurityContext $security) 
  { 
    $this->router = $router; 
    $this->security = $security; 
    echo "hello world";exit;
  } 

  public function onAuthenticationSuccess(Request $request, TokenInterface $token) 
  { 

    echo "hello world 2";exit;
    if ($this->security->isGranted('ROLE_SUPER_ADMIN')) 
    { 
      $response = new RedirectResponse($this->router->generate('category_index'));       
    } 
    elseif ($this->security->isGranted('ROLE_ADMIN')) 
    { 
      $response = new RedirectResponse($this->router->generate('category_index')); 
    }  
    elseif ($this->security->isGranted('ROLE_USER')) 
    { 
      // redirect the user to where they were before the login process begun. 
      $referer_url = $request->headers->get('referer'); 

      $response = new RedirectResponse($referer_url); 
    } 

    return $response; 
  } 

}

But if I execute the web app, it doesn’t into at this function and doesn’t show “Hello world”.

Do you know what I’m doing bad?
Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:21:02+00:00Added an answer on June 17, 2026 at 12:21 pm

    I found the UrlGeneratorInterface used in FOSUserBundle

    FosUserBundle Hooking into controllers

    use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
    ...
    
    public function __construct(UrlGeneratorInterface $router)
    {
        $this->router = $router;
    }
    ...
    ...
    
    $url = $this->router->generate('homepage');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Devise gem and I want to change the redirect path after
I have 2 domain domain1.com domain2.net i want redirect domain1.com to domain2.net with path
Usually we can redirect to another page using ForwardResolution(path) in stripes, but I want
I want to redirect to another website outside of my domain, such as this:
I want to redirect my page to another if a condition is satisfied say,
I want to redirect this the request for homebox/1 to homebox/1/[uid] if a logged
I have a method that where I want to redirect the user back to
I want to redirect http://www.suma.ir/product.php?id_product=12 to http://www.suma.ir/product.php?id_product=508 but I'm having trouble. The URL path
I'm trying to redirect to another route using: $location.path(/route); But for some reason it
I am redirecting one domain to another, but I want to preserve the path

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.