【问题标题】:Symfony 2.1 - Creating authentication listener with JMS\DiExtraBundleSymfony 2.1 - 使用 JMS\DiExtraBundle 创建身份验证侦听器
【发布时间】:2012-08-16 23:46:44
【问题描述】:

我创建了以下监听器:

<?php
namespace KekRozsak\SecurityBundle\Security;

use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use JMS\DiExtraBundle\Annotation as DI;

/**
 * @DI\Service
 * @DI\Tag("kernel.event_listener", attributes={"event" = "security.authentication.success"})
 */
class AuthSuccess implements AuthenticationSuccessHandlerInterface
{
    /**
     * The Doctrine interface
     *
     * @var Symfony\Bridge\Doctrine\RegistryInterface $doctrine
     *
     * @DI\Inject
     */
    private $doctrine;

    public function onSecurityAuthenticationSuccess(AuthenticationEvent $event)
    {
        $user = $event->getAuthenticationToken()->getUser();
        $em = $this->doctrine->getEntityManager();
        $user->setLastLoginAt(new \DateTime('now'));
        $em->persist($user);
        $em->flush();
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        $user = $token->getUser();
        $em = $this->doctrine->getEntityManager();
        $user->setLastLoginAt(new \DateTime('now'));
        $em->persist($user);
        $em->flush();
    }
}

但它没有被调用。如果我注入它

    <service id="kek_rozsak_security.auth.success" class="KekRozsak\SecurityBundle\Security\AuthSuccess">
        <argument type="service" id="doctrine" />
        <tag name="kernel.event_listener" event="security.authentication.success" />
    </service>

并添加构造函数:

public function __construct(RegistryInterface $doctrine)
{
    $this->doctrine = $doctrine;
}

它像魅力一样运行。我错过了什么吗?

【问题讨论】:

  • 您是否记得将 all_bundles 设置为 true,或者明确配置此捆绑包以使用注释?应该在你的 config.yml 中设置。
  • 没有,只是发现了。我在一分钟内写下我对我的菜鸟的回答......:S

标签: php symfony-2.1


【解决方案1】:

回答我自己的(有点菜鸟)问题...

我只是忘了添加

jms_di_extra:
    locations:
        all_bundles: false
        bundles: [ KekRozsakFrontBundle, KekRozsakSecurityBundle ]
        directories: [ "%kernel.root_dir%/../src" ]

致我的config.yml

【讨论】:

    猜你喜欢
    • 2012-09-15
    • 2018-10-17
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 2012-06-22
    • 2021-04-26
    • 1970-01-01
    相关资源
    最近更新 更多