2013-04-04 73 views
3

我在嘗試使用ZF2的身份驗證服務時遇到了一些問題。我有以下Module.php getServiceConfig功能:ZF2 - ServiceManager&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,所以儘量不要太苛刻。

編輯:順便說一句,global.php文件包括在ZF2教程應用程序中說明的service_manager適配器工廠。

謝謝! 傑米Mclaughlan

+0

我有同樣的問題。你是如何解決這個問題的? – Pascal 2013-10-20 10:04:49

回答

0

你檢查這些:在您的module.config.php

  • autoload_classmap.php(爲模塊)
  • 這樣

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

    我希望它有幫助你找到答案

+0

嗨,我真的很感激你的回覆。 我的autoload_classmap.php只是一個空數組,module.config包含service_manager,其適配器包含在global.php文件中。我仍然不確定可能導致問題的原因。 – 2013-04-04 20:33:32

+0

另外,我能夠在模塊類中的一個AuthTableGateway工廠中創建一個適配器,但它不會允許我在AuthService類中創建另一個適配器。我很困惑! – 2013-04-04 21:12:36

相關問題