2017-06-02 45 views
2

登錄後,我想在我的類中創建當前用戶對象而不創建服務。 Becauase我已經創建了當前的用戶插件和幫手。沒有創建服務的類中的當前用戶對象服務

所以,如果我需要當前的用戶對象在我的課,那麼如何獲得當前的用戶對象。

即,

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

} 

在Zend框架2,我只是用:

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

我如何在Zend框架3這樣做呢?

回答

1

如ZF2,你應該有訪問控制器插件identity,如果你想在其他班級比控制器使用它使用它像$this->identity()

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

,比你需要它注入,如ZF2 。

相關問題