Php zf2 ServiceNotFoundException,如何修复
我正在使用zend框架,我在side model文件夹中的module src文件夹中创建了一个模型,我想将该模型用作服务,并在module.php配置中调用了该模型类,但当我运行应用程序时,它向我显示了错误ServiceNotFoundExceptionPhp zf2 ServiceNotFoundException,如何修复,php,zend-framework,Php,Zend Framework,我正在使用zend框架,我在side model文件夹中的module src文件夹中创建了一个模型,我想将该模型用作服务,并在module.php配置中调用了该模型类,但当我运行应用程序时,它向我显示了错误ServiceNotFoundException Zend\ServiceManager\ServiceManager::createFromInvokable:通过可调用类“Authentication\Model\AuthDb”检索“authdbservice(别名:authdbser
Zend\ServiceManager\ServiceManager::createFromInvokable:通过可调用类“Authentication\Model\AuthDb”检索“authdbservice(别名:authdbservice)”失败;类不存在
这是我的module.php文件
类模块实现AutoloaderProviderInterface
ConfigProviderInterface, ServiceProviderInterface
{
/**
*
* {@inheritDoc}
* @see \Zend\ModuleManager\Feature\AutoloaderProviderInterface::getAutoloaderConfig()
*/
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
/**
*
* {@inheritDoc}
* @see \Zend\ModuleManager\Feature\ConfigProviderInterface::getConfig()
*/
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
/**
*
* {@inheritDoc}
* @see \Zend\ModuleManager\Feature\ServiceProviderInterface::getServiceConfig()
*/
public function getServiceConfig()
{
return array(
'invokables' => array(
'AuthService' => 'Authentication\Service\AuthService',
'AuthDbService' => 'Authentication\DbService\AuthDb',
),
);
}
}
这是我的authDb类
namespace Authentication\DbService;
use Doctrine\ORM\EntityManager;
use Authentication\Entity\User;
use Authentication\Form\RegisterForm;
class AuthDb {
protected $em;
protected function getEntityManger() {
if (null === $this->em) {
$this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
}
return $this->em;
}
public function submitNewUser() {
$form = new RegisterForm();
$form->get('submit')->setValue('Register');
$request = $this->getRequest();
if ($request->isPost()) {
$user = new User();
$form->setInputFilter($user->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$user->exchangeArray($form->getData());
$user->__set('password', md5($user->__get('password')));
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
return $this->redirect()->toRoute('auth', array('action' => 'login'));
} else {
return array('form' => $form, 'messages' => $form->getMessages());
}
}
}
}
我的目录和文件结构是
./Authentication
./Authentication/autoload_classmap.php
./Authentication/Module.php
./Authentication/view
./Authentication/view/authentication
./Authentication/view/authentication/auth
./Authentication/view/authentication/auth/register.phtml
./Authentication/view/authentication/auth/login.phtml
./Authentication/config
./Authentication/config/module.config.php
./Authentication/src
./Authentication/src/Authentication
./Authentication/src/Authentication/Service
./Authentication/src/Authentication/Service/AuthService.php
./Authentication/src/Authentication/Controller
./Authentication/src/Authentication/Controller/AuthController.php
./Authentication/src/Authentication/Model
./Authentication/src/Authentication/Model/AuthDbService.php
./Authentication/src/Authentication/Entity
./Authentication/src/Authentication/Entity/User.php
./Authentication/src/Authentication/Form
./Authentication/src/Authentication/Form/LoginForm.php
./Authentication/src/Authentication/Form/RegisterForm.php
我认为在这种情况下,我的配置中存在一些问题
有没有办法解决这个问题?我刚刚找到了解决方案,我不得不承认这对我来说非常复杂,但现在它更容易了一点, 由于我希望在控制器和其他服务中可以访问我的数据库模型作为服务,因此我必须通过使用设计模式的一些大方面(在本例中为工厂)进行一些小的更改。 所以我把AuthDbService.php文件放在Model文件夹中,但它没有正确的名称空间,所以我不得不将名称空间更改为
名称空间身份验证\Model
(这是正确的名称空间)
我还将类名从AuthDb
更改为authdservice
在那之后,我可以在我的另一个控制器中使用这个。
我将我的getServiceConfig
函数更改为
public function getServiceConfig()
{
return array(
'invokables' => array(
'AuthService' => 'Authentication\Service\AuthService',
'AuthDbService' => 'Authentication\Model\AuthDbService',
)
);
}
现在我可以在我的控制器中使用它了,但事情就是这样!
我想在authdservice
中使用另一个服务(AuthService
),这是新问题。因为我不能在我的authdservice
中使用authdservice
为什么?AuthService
是一种只能从Zend ServiceManager访问的服务,为此,我需要访问AuthDbService类中的ServiceManager,由于它是一个模型而不是控制器,并且不是从AbstractActionController
扩展而来,从技术上讲,我无权访问ServiceManager来注入AuthService
因此,我必须首先在我的ServiceModel(AuthDbService
)中插入ServiceManager,为此,我必须再次在Module.phpgetServiceConfig
函数中进行一些更改
但在此之前,我在我的authdservice
类中添加了这些代码行
protected $serviceLocator;
/**
* serviceLocator Setter
* @param ServiceLocatorInterface $serviceLocator
* @author Ehsan Aghaei
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}
/**
* serviceLocator Getter
* @return \Authentication\Model\Service\unknown
* @author Ehsan Aghaei
*/
protected function getServiceLocator()
{
return $this->serviceLocator;
}
我只是让setter和getter将ServiceManger对象注入这个类中。
之后,我对getServiceConfig
进行了如下更改
public function getServiceConfig()
{
return array(
'invokables' => array(
'AuthService' => 'Authentication\Service\AuthService',
),
'factories' => array(
'AuthDbService' => function ($sm) {
$authService = new AuthDbService();
$authService->setServiceLocator($sm);
return $authService;
}
),
);
}
现在我可以访问service Manager,并且可以从service Manager访问其他服务。
这就是我解决问题的方法。
我希望它能帮助像我这样的人