【问题标题】:ZF2 - ServiceManager & getServiceConfig problems - unable to fetch or create instanceZF2 - ServiceManager & getServiceConfig 问题 - 无法获取或创建实例
【发布时间】:2013-03-26 18:47:52
【问题描述】:

我在尝试使用 ZF2 的身份验证服务时遇到了一些问题。我必须关注 Module.php getServiceConfig 函数:

<?php

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Auth\Model\CustomerTable' =>  function($sm) {
                $tableGateway = $sm->get('CustomerTableGateway');
                $table = new CustomerTable($tableGateway);
                return $table;
            },
            'CustomerTableGateway' => function($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Customer()); // prototype pattern implemented.
                return new TableGateway('customer', $dbAdapter, null, $resultSetPrototype);
            },
            'Auth\Model\AuthStorage' => function($sm){
                return new \Auth\Model\AuthStorage('jamietech');  
            },
            'AuthService' => function($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $dbTableAuthAdapter  = new DbTableAuthAdapter($dbAdapter, 
                                          'customer','username','password');

                $authService = new AuthenticationService();
                $authService->setAdapter($dbTableAuthAdapter);
                $authService->setStorage($sm->get('Auth\Model\AuthStorage'));

                return $authService;
            },
        ),
    );
}

AuthStorage 工厂只是为我们创建了一个新的 AuthStorage 来跟踪我拥有的 rememberMe 功能,而 AuthService 工厂为我们创建了一个新的身份验证服务。我看不到我做错了什么,但是在 AuthController.php 中运行以下代码时:

<?php

public function loginAction()
{
    //if already login, redirect to success page 

    if ($this->getAuthService()->hasIdentity()){
        return $this->redirect()->toRoute('success');
    }

    $form = new LoginForm();
    return array(
        'form'     => $form,
        'messages' => $this->flashmessenger()->getMessages()
    );
}

public function logoutAction()
{
    $this->getSessionStorage()->forgetMe();
    $this->getAuthService()->clearIdentity();
    $this->flashmessenger()->addMessage("You have logged out successfully.");

    return $this->redirect()->toRoute('auth', array('action'=>'login'));
}

PHPUnit 在运行 PHPUnit 命令时遇到以下错误:

1: "testLoginActionCanBeAccessed - Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance of Zend\Db\Adapter\Adapter

1: "testLogoutActionCanBeAccessed - session_regenerate_id(): cannot regenerate session id - headers already sent.

运行 -process-isolation 命令时,登录和注销都会出现此错误:

"Serialization of closure is not allowed in: C;\Users\-----\AppData\Local\Temp\-----

如果有人可以提供帮助,那就太好了。我是 ZF 菜鸟,所以尽量不要太苛刻。

编辑:顺便说一句,global.php 文件包括 ZF2 教程应用程序中说明的 service_manager 适配器工厂。

谢谢! 杰米麦克劳伦

【问题讨论】:

  • 我也有同样的问题。你是怎么解决这个问题的?

标签: phpunit zend-framework2


【解决方案1】:

你检查过这些吗:

  • autoload_classmap.php(用于您的模块)
  • 在你的 module.config.php

喜欢这个

service_manager' => array(
 'aliases' => array(    
  'mymodule-ZendDbAdapter' => 'Zend\Db\Adapter\Adapter',
  ),
);

希望能帮助你找到答案

【讨论】:

  • 您好,非常感谢您的回复。我的 autoload_classmap.php 只是一个空数组,module.config 包含 service_manager ,它的适配器包含在 global.php 文件中。我仍然不确定是什么导致了这个问题。
  • 另外,我可以在模块类中的一个我的 AuthTableGateway 工厂中创建一个适配器,但它不允许我在 AuthService 类中创建另一个。我很困惑!
猜你喜欢
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
相关资源
最近更新 更多