Skip to content

Instantly share code, notes, and snippets.

@snc
Created October 25, 2011 13:44
Show Gist options
  • Save snc/1312769 to your computer and use it in GitHub Desktop.
Save snc/1312769 to your computer and use it in GitHub Desktop.
Custom FOSUB redirects
<?php
namespace My\Bundle\EventListener;
use My\Bundle\User\User;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class InteractiveLoginListener
{
/**
* @var \Symfony\Component\Routing\Router
*/
private $router;
/**
* @param \Symfony\Component\Routing\Router $router
*/
public function __construct(Router $router)
{
$this->router = $router;
}
/**
* @param \Symfony\Component\Security\Http\Event\InteractiveLoginEvent $event
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$user = $event->getAuthenticationToken()->getUser(); /** @var \My\Bundle\User\User */
$request = $event->getRequest(); /** @var \Symfony\Component\HttpFoundation\Request $request */
if ($user instanceof User) {
$request->request->set('_target_path', $this->router->generate('my_custom_route', array('_locale' => 'en')));
}
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="my.security.interactive_login_listener" class="My\Bundle\EventListener\InteractiveLoginListener">
<argument type="service" id="router" />
<tag name="kernel.event_listener" event="security.interactive_login" method="onSecurityInteractiveLogin" priority="255" />
</service>
</services>
</container>
@garak
Copy link

garak commented Apr 9, 2019

Setting _target_path is not working for me. Symfony 3.4.18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment