【问题标题】:zfcUser getState in another modulezfcUser getState 在另一个模块中
【发布时间】:2013-04-25 04:44:13
【问题描述】:

我如何从 zfcUser 获取状态

在 view/index.phtml 我从 $this->zfcUserIdentity()->getState();

但现在我需要在其他模块/控制器(这是我的成本模块控制器)中获取此值(已登录用户的状态)

所以我需要从以下位置获取状态: zfcUser/实体/用户 至 我的模块/控制器

我看了这个https://github.com/ZF-Commons/ZfcUser/wiki/How-to-check-if-the-user-is-logged-in,但这个解决方案没有帮助

【问题讨论】:

    标签: module zend-framework2 zfcuser


    【解决方案1】:

    这对我也有帮助:

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

    【讨论】:

      【解决方案2】:

      state 是用户本身的属性。因此,如果您通过识别服务获取用户,则可以从那里获取状态。

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

      请注意,当用户登录时,if 条件为假。状态也可以是null 或任意值,所以不要期望每个用户都返回一个有效的状态(换句话说,检查返回的值!)

      【讨论】:

        【解决方案3】:

        按照这段代码,我遇到了同样的问题,然后我已经管理了如何通过 zfcUser 使用登录用户的身份

        在顶部的其他模块控制器中,

          use Zend\EventManager\EventManagerInterface;
        

        然后在同一个类中创建这两个函数,

        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] => rajat.modi@softwebsolutions.com
        [displayName:protected] => 
        [password:protected] => $2y$14$2WxYLE0DV0mH7txIRm7GkeVJB3mhD4FmnHmxmrkOXtUFL7S9PqWy6
        [state:protected] => 
        )
        

        就是这样,无论用户是否登录,您都会自动获取身份,否则它将重定向到登录页面。

        希望这会有所帮助

        【讨论】:

        • sorry no,我只需要getState表单用户登录,如果没有,那么它给我NULL
        • 也许我必须使用 BjyAuthorize ?
        • 我尝试将它放在其他控制器中:使用 ZfcUser\Entity;然后在函数中: $state = $this->getState();但它给了我一个错误,请帮助
        • 我更新了我的代码,请检查它并让我知道它是否有效......它已经过我的测试......
        • 是状态显示为空白,因为在数据库中它是空列,这就是为什么它是空字符串。插入一些它将在该字段中显示的数据。希望这是有道理的......
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-15
        • 2022-01-08
        • 2020-04-17
        • 1970-01-01
        • 2015-12-11
        • 1970-01-01
        相关资源
        最近更新 更多