简体   繁体   English

如何获取当前用户ID zf2

[英]how to get current user id zf2

I'm trying to get the current user using indexAction() function in My controller. 我正在尝试使用My Controller中的indexAction()函数来获取当前用户。 I get this error message: 我收到此错误消息:

Zend\\Mvc\\Controller\\PluginManager::get was unable to fetch or create Zend \\ Mvc \\ Controller \\ PluginManager :: get无法获取或创建
an instance for zfcUserIdentity zfcUserIdentity的实例

I'm using this statement to get the current user id: 我正在使用以下语句来获取当前用户ID:

$id =(int) $this->zfcUserIdentity()->getid() ;

zfcUserIdentity is a view helper not a controller plugin. zfcUserIdentity是视图帮助程序,而不是控制器插件。 Instead try this: 而是尝试以下方法:

$this->zfcUserAuthentication()->getIdentity();

This will get the currently logged in user if any. 这将获取当前登录的用户(如果有)。 If you want to check if a user is logged in use this: 如果要检查用户是否已登录,请使用以下命令:

if ($this->zfcUserAuthentication()->hasIdentity())

尝试:

$id = $this->UserAuthentication()->getIdentity()->getId();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM