Symfony 在用户登录后调用方法
我想知道在用户登录后是否有其他方法可以调用函数 下面是我想调用的代码:Symfony 在用户登录后调用方法,symfony,fosuserbundle,Symfony,Fosuserbundle,我想知道在用户登录后是否有其他方法可以调用函数 下面是我想调用的代码: $point = $this->container->get('process_points'); $point->ProcessPoints(1 , $this->container); 您可以在中找到SuserBundle Fire事件。更具体地说,这就是您正在寻找的: /** * The SECURITY_IMPLICIT_LOGIN event occurs when the user i
$point = $this->container->get('process_points');
$point->ProcessPoints(1 , $this->container);
您可以在中找到SuserBundle Fire事件。更具体地说,这就是您正在寻找的:
/**
* The SECURITY_IMPLICIT_LOGIN event occurs when the user is logged in programmatically.
*
* This event allows you to access the response which will be sent.
* The event listener method receives a FOS\UserBundle\Event\UserEvent instance.
*/
const SECURITY_IMPLICIT_LOGIN = 'fos_user.security.implicit_login';
可以在文档页面上找到与这些事件挂钩的文档。在您的情况下,您需要实现如下内容:
namespace Acme\UserBundle\EventListener;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
/**
* Listener responsible to change the redirection at the end of the password resetting
*/
class LoginListener implements EventSubscriberInterface
{
private $container;
public function __construct($container)
{
$this->container = $container;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onLogin',
SecurityEvents::INTERACTIVE_LOGIN => 'onLogin',
);
}
public function onLogin($event)
{
// FYI
// if ($event instanceof UserEvent) {
// $user = $event->getUser();
// }
// if ($event instanceof InteractiveLoginEvent) {
// $user = $event->getAuthenticationToken()->getUser();
// }
$point = $this->container->get('process_points');
$point->ProcessPoints(1 , $this->container);
}
}
然后,您应该将侦听器定义为服务并注入容器。或者,您可以只注入所需的服务,而不是整个容器
services:
acme_user.login:
class: Acme\UserBundle\EventListener\LoginListener
arguments: [@container]
tags:
- { name: kernel.event_subscriber }
还有另一种方法涉及到,但正如文档中所指出的,您必须复制他们的代码,这样,如果(或者更确切地说,当)FOSUserBundle发生更改,代码就不会完全干净,并且必然会被破坏。GitHub当前处于关闭状态,因此我无法提供完整答案,但这样做的方法是覆盖负责登录的FOSUserBundle控制器。谢谢,真的谢谢。。。我一个人是不会想到这一点的!有没有办法监听“自动登录”?而不是只在用户单击登录时才收听。示例:用户登录(使用RememberMe),下次他将访问该页面时,他将已经登录,但我想听听这个。我知道我可以监听controller并检查用户是否存在于安全上下文中,但我想监听symfony的“安全”部分。注入容器是不好的。相反,应该注入
process\u points
服务。@danield“或者,您可以只注入所需的服务,而不是整个容器。”@danield您完全正确,不幸的是,我已经多年没有使用Symfony2了。您可以自由编辑代码,只注入所需的内容。