Symfony2:在侦听器服务中拉取Request $请求

时间:2015-01-27 13:58:37

标签: symfony

我尝试在我的Symfony2项目中成功登录用户后设置一些数据,为此我需要传递参数Request $ request。 但是我得到了以下错误:

捕获致命错误:传递给Utilisateurs \ UtilisateursBundle \ Listener \ LoginListener :: onSecurityInteractiveLogin()的参数2必须是Symfony \ Component \ HttpFoundation \ Request的实例,给定字符串

以下是我的代码:

services.yml

services:
    login_listener:
        class: 'Utilisateurs\UtilisateursBundle\Listener\LoginListener'
        arguments: ['@security.context', '@doctrine']
        tags:
            - { name: 'kernel.event_listener', event: 'security.interactive_login' }
        scope: request

LoginListener:

<?php

namespace Utilisateurs\UtilisateursBundle\Listener;

use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\SecurityContext;

use Symfony\Component\HttpFoundation\Request as Request;
use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine; // for Symfony 2.1.0+

/**
 * Custom login listener.
 */
class LoginListener
{
    /** @var \Symfony\Component\Security\Core\SecurityContext */
    private $securityContext;

    /** @var \Doctrine\ORM\EntityManager */
    private $em;



    /**
     * Constructor
     * 
     * @param SecurityContext $securityContext
     * @param Doctrine        $doctrine
     */
    public function __construct(SecurityContext $securityContext, Doctrine $doctrine)
    {
        $this->securityContext = $securityContext;
        $this->em              = $doctrine->getEntityManager();
    }

    /**
     * Do the magic.
     * 
     * @param InteractiveLoginEvent $event
     */
    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event, Request $request)
    {
        if ($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
            // user has just logged in
        }

        if ($this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
            // user has logged in using remember_me cookie
        }

                    $session = $request->getSession();
                    $content = $session->get('result');
                    $idQuery = $session->get('query_id');
                    $authorId = $this->getUser()->getId();

                    $biblio = new Biblio;
                    $biblio->setAuthorId($authorId);
                    $biblio->setContent($content);
                    $biblio->setQueryId($idQuery);
                    $biblio->setDate(new \DateTime());
                    $em = $this->getDoctrine()->getManager();
                    $em->persist($biblio);
                    $em->flush();

                    $session->clear();

                    $response = $this->forward('BiblishareBundle:Profile:show');

                    return $response;

        // do some other magic here
        $user = $event->getAuthenticationToken()->getUser();

        // ...
    }
}

我正在使用FOSUserBundle。 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

尝试$request = $event->getRequest();

我不确定您是否在方法Request

中获得onSecurityInteractiveLogin

无论如何http://api.symfony.com/2.0/Symfony/Component/Security/Http/Event/InteractiveLoginEvent.html