我在嘗試使用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
我有同樣的問題。你是如何解決這個問題的? – Pascal 2013-10-20 10:04:49