如何重定向出事件?

时间:2013-09-25 12:01:18

标签: symfony

我正在收听security.authentication.success事件。每次客户登录时,我都需要检查他是否仍然掌握了主要信息。如果没有,那么我想将他重定向到创建表单。它运行良好,但我可以通过哪种方式重定向事件?简单地返回重定向对象不起作用。

服务:

applypie_userbundle.newuser_listener:
  class: Applypie\Bundle\UserBundle\EventListener\NewUserListener
  arguments: [@router]
  tags:
    - { name: kernel.event_listener, event: security.authentication.success, method: onLogin }

监听器:

namespace Applypie\Bundle\UserBundle\EventListener;

use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;

class NewUserListener
{
    protected $context;
    protected $router;

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

    /**
     * @param AuthenticationEvent $event
     *
     * if current user neither have an applicant row or an company row, redirect him to create applicant row
     */
    public function onLogin(AuthenticationEvent $event)
    {

        $user = $event->getAuthenticationToken()->getUser();

        if(!$user->getApplicant() && !count($user->getCompanies()))
        {
            return new RedirectResponse($this->router->generate('applypie_user_applicant_create'));
        }

    }
}

1 个答案:

答案 0 :(得分:1)

长话短说,是的,你正在听正确的事件,但......有人在等你的方法回归吗?... NOPE!

我现在无法测试,但这就是这个想法:

<强> security.yml

firewalls:
    main:
        pattern: ^/
        form_login:
            success_handler: my.security.login_handler

<强> services.yml

services:
    my.security.login_handler:
        class:  Applypie\Bundle\UserBundle\EventListener\NewUserListener
        arguments:  [@router]

<强> NewUserListener

namespace Applypie\Bundle\UserBundle\EventListener;

use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;


class NewUserListener implements AuthenticationSuccessHandlerInterface
{
    protected $router;

    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    protected function getResponse($name)
    {
        $url = $this->router->generate($name);
        $response = new RedirectResponse($url);

        return $response;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        $user = $token->getUser();
        $route = 'user_home';

        if(!$user->getApplicant() && empty($user->getCompanies())) {

            $route = 'applypie_user_applicant_create';

        }

        return $this->getResponse($route);
    }
}

来源:

http://api.symfony.com/2.3/Symfony/Component/Security/Http/Authentication/AuthenticationSuccessHandlerInterface.html

Do something just after symfony2 login success and before redirect?