【问题标题】:zf2 - zfcuser check auth for some module onlyzf2 - zfcuser 仅检查某些模块的身份验证
【发布时间】:2013-10-13 17:25:27
【问题描述】:

我是 zf2 的新手。 zfcuser 按照安装指南进行设置。我能够注册,登录和注销。

我已经为前端和后端创建了模块。我试图检查用户是否登录后端 - 管理员和所有子模块。

我试图包含

$sm  = $app->getServiceManager();
        $auth = $sm->get('zfcuser_auth_service');
        if (!$auth->hasIdentity()) {
            //redirect to login page
        } 

在我的 admin/module.php 函数 onBootStrap

它确实检查了登录,但不仅针对管理员,还针对包括前端在内的整个模块。

我只需要检查管理模块的登录,以及管理的所有子模块。

不知道怎么做。请帮忙

【问题讨论】:

    标签: authentication zend-framework2 zfcuser


    【解决方案1】:

    在你的module.config.php中

    <?php
    
    namespace AdminModule;
    use Zend\Authentication\AuthenticationService;
    
    return array(
        __NAMESPACE__ => array(
            'params' => array(),
            'service_manager' => array(
                'invokables' => array(
                   'Zend\Authentication\AuthenticationService' => 'Zend\Authentication\AuthenticationService',
                ),
            ),
            'services' => array(
                // Keys are the service names
                // Values are objects
                'Auth' => new AuthenticationService(),
            ),
        ),
    );
    

    控制器/管理器中的示例

        $this->auth = new AuthenticationService();
    
        if($this->auth->hasIdentity()){
            $this->userid = $this->auth->getIdentity();
        }
    

    你也可以使用工厂。

    控制器中的示例

    使用来自ZfcUser module的工具

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多