2012-12-23 21 views
2

我怎麼能由zfcUserzfcUser的getState另一個模塊

GETSTATE鑑於

/index.phtml我把它從這個 - $> zfcUserIdentity() - >的getState();

,但現在我需要得到這個值(狀態爲這個用戶是誰登錄),在其他模塊/控制器(這是我的costum模塊控制器)

,所以我需要從獲得國家: zfcUser /實體/用戶 到 Mymodule中/控制器

我看這個https://github.com/ZF-Commons/ZfcUser/wiki/How-to-check-if-the-user-is-logged-in但這solutons是沒有幫助的

回答

7

,這也有幫助,對我來說(換句話說,檢查返回值!):

$sm = $this->getServiceLocator(); 
$auth = $sm->get('zfcuserauthservice'); 
if ($auth->hasIdentity()) { 
    $user_edit = $auth->getIdentity()->getPrem(); 
} 
1

遵循這個代碼,我有同樣的問題,那麼我管理如何使用通過zfcUser在用戶登錄身份

在上部其他模塊控制器

use Zend\EventManager\EventManagerInterface; 

然後創建在sameclass這兩功能,

public function setEventManager(EventManagerInterface $events) 
{ 
    parent::setEventManager($events); 

    $controller = $this; 
    $events->attach('dispatch', function ($e) use ($controller) { 

     if (is_callable(array($controller, 'checkUserIdentity'))) 
     { 
      call_user_func(array($controller, 'checkUserIdentity')); 
     } 
    }, 100); 
} 

public function checkUserIdentity() 
{ 

    if ($this->zfcUserAuthentication()->hasIdentity()) { 
    echo "<pre>"; print_r($this->zfcUserAuthentication()->getIdentity());die; 

     } 

} 

它會給這種輸出

Admin\Entity\User Object 
(
[id:protected] => 2 
[username:protected] => 
[email:protected] => [email protected] 
[displayName:protected] => 
[password:protected] => $2y$14$2WxYLE0DV0mH7txIRm7GkeVJB3mhD4FmnHmxmrkOXtUFL7S9PqWy6 
[state:protected] => 
) 

的就是這樣您將自動獲取身份,無論用戶是否登錄,如果不是,那麼它將重定向到登錄頁面。

希望這將有助於

+0

對不起,我只需要登錄的getState表單用戶,如果不是,那麼它給我NULL –

+0

也許我必須使用BjyAuthorize? –

+0

我試着把它放在其他控制器中: 使用ZfcUser \ Entity; 然後在函數中: $ state = $ this-> getState(); 但它給我只是錯誤,請幫助 –

1

state是從用戶本身的屬性。所以如果你讓用戶通過身份識別服務,你可以從那裏獲取狀態。

public function myFooAction() 
{ 
    if ($this->zfcUserAuthentication()->hasIdentity()) { 
     $user = $this->zfcUserAuthentication()->getIdentity(); 
     $state = $user->getState(); 
    } 
} 

記住,當用戶登錄,if條件爲假。另外,狀態可以是null或任意值,所以不要指望每個用戶返回一個有效的狀態