Zend framework2 zf2获取登录到模型中系统的用户ID
我正在使用ZF2 我想获取已登录系统的用户信息。基本上,每个表都有一个create_userid和last_update_userid。我想用已登录并执行操作的用户的id来填充它 我可以将其作为参数从控制器传递到我的操作中;我想从系统中自动获取此信息Zend framework2 zf2获取登录到模型中系统的用户ID,zend-framework2,Zend Framework2,我正在使用ZF2 我想获取已登录系统的用户信息。基本上,每个表都有一个create_userid和last_update_userid。我想用已登录并执行操作的用户的id来填充它 我可以将其作为参数从控制器传递到我的操作中;我想从系统中自动获取此信息 我相信其他人也会想到这一点并执行这一操作。我做了一些研究,发现当创建一个类来实现ServiceLocatorAwareInterface时,我们可以使用ServiceLocator做各种事情。此外,servicelocator由MVC引擎自动设置
我相信其他人也会想到这一点并执行这一操作。我做了一些研究,发现当创建一个类来实现ServiceLocatorAwareInterface时,我们可以使用ServiceLocator做各种事情。此外,servicelocator由MVC引擎自动设置 我创建该表是为了实现ServiceLocatorAwareInterface,我可以获取登录用户的用户id 我有下面的代码
namespace UserAdmin\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class UserTable implements ServiceLocatorAwareInterface {
protected $serviceLocator = null;
protected $loggedUser = 0;
protected $tableGateway;
protected $table = "user";
public function __construct( TableGateway $tableGateway ) {
$this->tableGateway = $tableGateway;
}
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
}
public function getServiceLocator() {
return $this->serviceLocator;
}
protected function setLoggedUser( ) {
$serviceLocator = $this->getServiceLocator( );
$authService = $serviceLocator->get( 'AuthService' );
if ( $authService->hasIdentity( ) ) {
$this->loggedUser = $authService->getIdentity( )->user_id;
}
}
...
...
public function saveUser ( User $user ) {
if ( $this->loggedUser == 0 ) {
$this->setLoggedUser( );
}
...
...
$data [ 'last_update_userid' ] = $this->loggedUser;
$data [ 'last_update_timestamp' ] = date ( 'Y-m-d H:i:s' );
if ( is_null( $user_id ) ) {
$data [ 'create_userid' ] = $this->loggedUser;
$data [ 'create_timestamp' ] = date ( 'Y-m-d H:i:s' );
$this->tableGateway->insert( $data );
} else {
if ( $this->getUser ( $user_id ) ) {
$this->tableGateway->update( $data, array ( 'user_id' => $user_id, ) );
} else {
throw new \Exception ( "Could not find row in table $this->table for update" );
}
}
}
}
这满足了我的大部分需求。只有两个方面
a> 我尝试从构造函数调用setLoggedUser函数,但它不起作用。我认为服务定位器是在整个施工之后设置的。所以我在保存之前调用这个函数,那是我需要它的时候。这是一个干净的实现,还是有其他方法可以做到这一点
b> 用户可以在网站上注册自己;而且userid是一个自动生成的序列,所以我如何填充userid使其与新的userid相同
我想到两件事;
1> 插入后,读取用户记录并使用userid更新记录
2> 不用太担心这一点,可以使用系统超级管理员id作为用户自注册的id
这是一个问题,仅适用于用户的自我注册。只有在用户登录后,才会插入或更新系统中的任何其他表。在UserTable类中设置服务定位器- 在UserAdmin/Module.php中-
'UserAdmin\Model\UserTable' => function($sm) {
$tableGateway = $sm->get('UserTableGateway');
$table = new UserTable($tableGateway);
//This way the serviceLocator is injected to the Table.
$table->setServiceLocator($sm);
return $table;
},
现在,当您从控制器调用setLoggedUser时,将设置该值
重要:
最好的方法是使用控制器插件,而不是使用上述私有功能。此插件将检查是否有用户登录,如果是,则返回用户id。身份验证帮助程序和控制器插件是您要寻找的:@foozy我不是说在控制器中获取用户信息。在关于将用户信息传递到模型中以执行审计跟踪的web讨论中,我需要这些信息;像我们使用服务管理器注入TableGateway一样,将用户信息注入类中是否好。这种方法的缺点是什么?不用太担心,可以使用系统superadmin id作为写入自注册用户的id。似乎是对的。但在执行exchangeArray时,只需从控制器设置create_userid,这样用户$User对象就可以拥有它,这始终是一个好的做法。不要为了这么小的事情而将serviceLocator包含在User Table类中。@kunaldesethe这会影响性能吗?现在,我的控制器不知道审计列。当您从控制器调用setLoggedUser时,它没有设置serviceLocator。您需要做的是从Module.php中注入$sm,类似于tableGateway的注入方式。请检查新答案。是的,分配给表类的serviceLocator确实有问题。它只是让表能够完全访问所有服务。依赖注入的整个概念都消失了。您永远无法清楚地了解Table类在多大程度上依赖于其他类/服务;框架直接注入了这一点。自动设置不是更好吗?