我在 Zend Framework 2 中使用 zfcUser 。
我想捕获登录事件,以便我可以对其执行某些操作,并将用户重定向到其他页面。
我知道如何捕捉注册事件。即我在我的module.php文件中放置一个onBootstrap,然后执行以下操作
$em->attach('ZfcUser\Form\RegisterFilter','init',function($e)
整个功能都是这样的;
public function onBootstrap(MVCEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$em = $eventManager->getSharedManager();
$em->attach(
'ZfcUser\Form\RegisterFilter',
'init',
function($e)
{ }
}
然而,当我试图抓住登录事件时,我没有任何结果。即
$zfcServiceEvents->attach('login.post', function($e) {
echo "loggedn"; die();
});
真的很感激一些帮助。
谢谢你。编辑:
这就是我调用注册事件的方式(我知道如何为刚刚注册的用户调用该事件。但我想在用户刚刚成功登录时调用该事件
公共功能onBootstrap(MVCEvent $ e) {
$zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager();
$zfcServiceEvents->attach('register', function($e) {
$form = $e->getParam('form');
$user = $e->getParam('user');
对cptnk CODE的回应
响应cptnk给出的答案,我尝试了以下代码,但它没有用;
public function onBootstrap(MVCEvent $e)
{
$sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
$serviceManager = $e->getApplication()->getServiceManager();
$loginFunction = function ($e) use ($serviceManager) {
echo "eventCaptured" ; die();
};
$sharedManager->attach('ZfcUser\Service\User', 'login.post', $loginFunction);
}
我仍然无法捕获登录事件。有什么想法吗?
cptnik提出了一个很好的建议;他问我是否覆盖了ZFcUser服务。在回答我澄清说我已经覆盖了zfcuser。以下是我使用的代码;'zfcuser' => array(
// telling ZfcUser to use our own class
'user_entity_class' => 'BaseModel\Entity\User',
// telling ZfcUserDoctrineORM to skip the entities it defines
'enable_default_entities' => false,
),
答案 0 :(得分:2)
/**
* Listen to the bootstrap event
*
* @return array
*/
public function onBootstrap(MvcEvent $e)
{
$serviceManager = $e->getApplication()->getServiceManager()
$loginFunction = function ($e) use ($serviceManager) {
// do something
};
$sharedManager->attach('ZfcUser\Service\User', 'login.post', $loginFunction);
}
您的代码没有显示$zfcServiceEvents
是什么或来自哪里我假设它是一个引用zfcuser事件的事件管理器。由于我将事件附加到zf2共享管理器,因此我的方法略有不同。也许你对这段代码有疑问?
您可以像这样抓取sharedEventManager
:
$sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
编辑:我对如何在Module.php onBootstrap
函数中获取$ serviceManager和应用程序有点不清楚。
答案 1 :(得分:0)
试试这个
$events = $e->getApplication()->getEventManager()->getSharedManager();
// Handle login
$events->attach('ZfcUser\Authentication\Adapter\AdapterChain', 'authenticate.success', function($e) {
$userId = $e->getIdentity();
// do some stuff
});
答案 2 :(得分:0)
在你的Module.php中添加:
public function onBootstrap(MvcEvent $e) {
$em = $e->getApplication()->getEventManager();
$UserListener = $e->getApplication()->getServiceManager()->get('UserListener');
$em->attachAggregate($UserListener);
}
并使用以下代码创建src / User / Listener / UserListener.php:
namespace LcUser\Listener;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class LcUserListener extends AbstractListenerAggregate implements ServiceLocatorAwareInterface {
/**
* @var ServiceLocatorInterface
*/
protected $serviceManager;
/**
*
* @param ServiceLocatorInterface $serviceLocator
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceManager = $serviceLocator;
}
/**
*
* @return \Zend\ServiceManager\ServiceLocatorInterface
*/
public function getServiceLocator() {
return $this->serviceManager;
}
public function attach(EventManagerInterface $events) {
$sharedManager = $events->getSharedManager();
$this->listeners[] = $sharedManager->attach('ZfcUser\Authentication\Adapter\AdapterChain', 'authenticate.success', array($this, 'userLog'));
}
public function userLog(Event $e) {
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
$userlog = new UserLogin(); // User Login is my User Log table to record login details as below
$user = $em->getRepository('MyUser\Entity\User')
->findOneBy(array('id' => $e->getIdentity()));
$userlog->setGroupId($user->getId());
$userlog->setUserId($e->getIdentity());
$userlog->setEmail($_POST['identity']);
$userlog->setIpAddresses($_SERVER['SERVER_ADDR']);
$em->persist($userlog);
$em->flush();
}
}