gpt4 book ai didi

zend-framework3 - 没有创建服务的类中的当前用户对象

转载 作者:行者123 更新时间:2023-12-02 03:03:29 25 4
gpt4 key购买 nike

登录后,我希望我的类中的当前用户对象不创建服务。因为我已经创建了当前用户插件和助手。

所以如果我需要我的类中的当前用户对象,那么如何获取当前用户对象。

class CoreFunction
{
public static curentUserData()
{
// i want user object here
}

}

在 Zend Framework 2 中,我只是使用了:

class CoreFunction
{
public static curentUserData() {
$authService = $this->getServiceLocator()
->get('Zend\Authentication\AuthenticationService')
->getIdentity();
}
}

我如何在 Zend Framework 3 中执行此操作?

最佳答案

与 ZF2 一样,您应该可以访问 Controller 插件 identity并像 $this->identity()

一样使用它
// From the doc.
public function testAction()
{
if ($user = $this->identity()) {
// someone is logged !
} else {
// not logged in
}
}

如果你想在 Controller 之外的其他类中使用它,那么你需要注入(inject)它,就像在 ZF2 中一样。

关于zend-framework3 - 没有创建服务的类中的当前用户对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44330419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com