ZF2 - ServiceManager& getServiceConfig问题 - 无法获取或创建实例

时间:2013-04-04 15:32:17

标签: phpunit zend-framework2

我在尝试使用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 noob,所以尽量不要太苛刻。

编辑:BTW global.php文件包含ZF2教程应用程序中说明的service_manager适配器工厂。

谢谢! 杰米麦克劳兰

1 个答案:

答案 0 :(得分:0)

你检查了这些:

  • autoload_classmap.php(适用于您的模块)
  • 您在 module.config.php
像这样

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

我希望它能帮助你找到答案