我在尝试使用 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 适配器工厂。
谢谢!杰米麦克劳伦