если доступ индексной страницы Войти Я получаю эту ошибку:Вы забыли оператор «use» для другого пространства имен на службе?
ClassNotFoundException in appDevDebugProjectContainer.php line 1729:
Attempted to load class "LoginListener" from namespace "Chris\UserBundle".
Did you forget a "use" statement for another namespace?
Интересно, что ошибка приходит. Я никогда не реализовал сервис syfmfony2.
Вот мои источники:
мой config.yml
login_listener:
class: 'Chris\UserBundle\LoginListener'
arguments: ['@security.context', '@doctrine']
tags:
- { name: 'kernel.event_listener', event: 'security.interactive_login' }
мой Chris/UserBundle/Controller/LoginListener.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();
// ...
}
}
мой UserBundle.php
<?php
/**
* Created by PhpStorm.
* User: christianschade
* Date: 24.01.15
* Time: 15:10
*/
namespace Chris\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class UserBundle extends Bundle {
}
Спасибо за ваш шлем п!
благодаря работает нормально, но как мне получить доступ к $ this-> getDoctrine() из LoginListener.php? –
Вы ввели в класс конструктора, поэтому можете удалить строку, которую вы указали в комментарии, и просто использовать '$ this-> em-> getRepository ...' – Matteo
спасибо, что это работает как шарм –