gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:26:32 25 4
gpt4 key购买 nike

我在尝试使用 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 工厂为我们创建了一个新的 Authentication Service。我看不到我做错了什么,但是在 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 适配器工厂。

谢谢!杰米麦克劳伦

最佳答案

你检查过这些吗:

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

像这样

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

希望能帮到你

关于phpunit - ZF2 - ServiceManager 和 getServiceConfig 问题 - 无法获取或创建实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15815542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com