【问题标题】:Cannot find event from looking at ZfcUser User Mapper code无法通过查看 ZfcUser 用户映射器代码找到事件
【发布时间】:2017-01-19 10:16:19
【问题描述】:
我正在阅读ZfcUser Zend Framework Module 中的UserMapper 代码,因为我认为我可以在构建我的系统之前了解使用zends auth 系统的一般概念并且对某些事情感到好奇。
在它 (click here) 的第 19 行,我们有以下代码:
$this->getEventManager()->trigger('find', $this, array('entity' => $entity));
我真的很想知道在哪里可以找到正在触发的“查找”事件,或者它是将来可能查找事件的占位符
我查看了模块文件和其他一些文件,但找不到它
【问题讨论】:
标签:
php
zend-framework
zend-framework2
zfcuser
zend-framework3
【解决方案1】:
Zfcuser 是一个非常完整的模块,具有许多在普通基本身份验证中不需要的功能。这些功能包括ZfcUser 触发的几个事件,但它并不需要它,它只是在这里以防用户需要对此事件执行特定操作。
更多,ZfcUser可以与BjyAuthorize和roleuserbridge等其他模块一起使用,提供更多功能(acl和用户角色管理...),这些第三方模块可以使用@触发的事件987654327@.
例如,您想记录每个用户身份验证,您可以在您的onBootstrap() 方法中以这种方式使用authenticate 事件:(来自here 的示例):
$sm = $e->getApplication()->getServiceManager();
$zfcAuthEvents = $e->getApplication()->getServiceManager()->get('ZfcUser\Authentication\Adapter\AdapterChain')->getEventManager();
$zfcAuthEvents->attach( 'authenticate', function( $authEvent ) use( $sm ){
try {
$remote = new \Zend\Http\PhpEnvironment\RemoteAddress;
$remote->setUseProxy( true );
$mapper = $sm->get( 'auth_log_mapper' );
$log_entity = new \FooUser\Entity\UserAuthenticationLog;
$log_entity->populate(
array(
'user_id' => $authEvent->getIdentity(),
'auth_time' => new \DateTime( "now" ),
'ip_address' => $remote->getIpAddress()
)
);
$mapper->save( $log_entity );
return true;
} catch( \Exception $x ) {
// handle it
}
});
所以find 事件可能对ZfcUser 没有用处,它不是这个模块未来开发的占位符,它是你的一个钩子,如果你需要在找到用户时做某事用户存储库...