你忘记了"使用"服务上另一个命名空间的语句?

时间:2015-07-22 10:27:06

标签: php class symfony service

如果访问索引登录页面,我会收到此错误:

for (var i = 0; i < data.data.length; i++) {
//                  ^^^^^^^^^^^^^^^^^
    // Code Here
}

我想知道错误来自何处。我从未实现过syfmfony2服务。

以下是我的消息来源:

我的config.yml

ClassNotFoundException in appDevDebugProjectContainer.php line 1729:
Attempted to load class "LoginListener" from namespace "Chris\UserBundle".
Did you forget a "use" statement for another namespace?

我的Chris / UserBundle / Controller / LoginListener.php

login_listener:
    class: 'Chris\UserBundle\LoginListener'
    arguments: ['@security.context', '@doctrine']
    tags:
    - { name: 'kernel.event_listener', event: 'security.interactive_login' }

我的UserBundle.php

  <?php
/**
 * Created by PhpStorm.
 * User: christian
 * Date: 22.07.15
 * Time: 11:38
 */

namespace Chris\UserBundle\Controller;

use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\SecurityContext;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine; // for Symfony 2.1.0+
// use Symfony\Bundle\DoctrineBundle\Registry as Doctrine; // for Symfony 2.0.x
/**
 * Custom login listener.
 */
/**
 * @Route("blub", name="blub")
 */
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)
    {
        if ($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
            $userId = 1217;
            $em = $this->getDoctrine()->getManager();
            $user = $em->getRepository('UserBundle:User')->find($userId);
            $user->setLetzterLogin(new \DateTime("now"));
            $em->flush();
        }

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

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

        // ...
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您的服务定义报告错误的命名空间,缺少Controller路径,请尝试使用以下配置:

login_listener:
    class: 'Chris\UserBundle\Controller\LoginListener'
    arguments: ['@security.context', '@doctrine']
    tags:
    - { name: 'kernel.event_listener', event: 'security.interactive_login' }

当然,我不认为这个班级是真正的控制器