ZF2 注册了一个无效的工厂
ZF2 An Invalid Factory Was Registered
我在 ZF2 应用程序中有以下 类 和我的模块配置,它给出了以下错误:
While attempting to create applicationformuserform(alias: Application\Form
\UserForm) an invalid factory was registered for this instance type.
UserFormFactory.php
<?php
namespace Application\Factory\Form;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Application\Form\UserForm;
class UserFormFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $serviceLocator) {
$services = $serviceLocator->getServiceLocator();
$entityManager = $services->get('Doctrine\ORM\EntityManager');
$form = new UserForm($entityManager);
return $form;
}
}
?>
UserForm.php
<?php
namespace Application\Form;
use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;
use Doctrine\ORM\EntityManager;
class UserForm extends Form implements InputFilterProviderInterface {
protected $entityManager;
public function __construct(EntityManager $entityManager) {
parent::__construct();
$this->entityManager = $entityManager;
}
public function init() {
$this->add(array(
'name' => 'username',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'User Name',
),
));
$this->add(array(
'name' => 'first_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'First Name',
),
));
$this->add(array(
'name' => 'last_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Last Name',
),
));
$this->add(array(
'name' => 'role_id',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => array(
'object_manager' => $this->entityManager,
'target_class' => 'Application\Entity\Role',
'property' => 'id',
'is_method' => true,
'find_method' => array(
'name' => 'getRoles',
),
'label' => 'User Role',
),
));
}
public function getInputFilterSpecification() {
return array(); // filter and validation here
}
}
?>
Module.config.php
'form_elements' => array(
'factories' => array(
'Application\Form\UserForm' => 'Application\Factory\Form\UserFormFactory',
),
),
我正在另一个控制器工厂中使用这个表单工厂
UserControllerFactory.php
<?php
namespace Member\Factory\Controller;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Member\Controller\UserController;
use Application\Form\UserForm;
class UserControllerFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $serviceLocator) {
$services = $serviceLocator->getServiceLocator();
$userForm = $services->get('FormElementManager')->get('Application\Form\UserForm');
$controller = new UserController($userForm);
return $controller;
}
}
?>
谁能告诉我可能是什么问题?
看了一遍又一遍的代码,我自己找到了答案。实际上我的 Factory 和 Form 文件夹在 src 文件夹之外,这就是 Zend 找不到所有 类 两个文件夹。
我移动了 src 中的 Factory 和 Form 文件夹,现在它工作正常。
我遇到了类似的问题。我对我的工厂 classes 进行了一些更改(重构 + 较小的 class 名称更改)。结果是因为我使用的是 Classmap Autoloader ...而忘记在模块结构中重新 运行 php vendor/bin/classmap_generator.php ...新重命名的 class没有找到。可惜没有生成 "class not found" 错误。
未找到您的工厂。
在其他答案中检查您是否在控制器中使用 PSR-4 或 PSR-0
简要
- 您是否正确命名了工厂(没有拼写错误)?
- 您的 composer.json 是否使用模块的 PSR-0 或 PSR-4 命名空间更新?
- 你 运行
composer dump-autoload
了吗?
- 您的
autoload_classmap.php
是否包含过时的条目并混淆了自动加载器?
- 检查您的文件夹结构和名称
- 确保你的工厂
implements FactoryInterface
问问自己 "Why is my Factory class not being found when I have placed it right there" 显然必须在哪里找到它?这将帮助您找到问题所在。
我在 ZF2 应用程序中有以下 类 和我的模块配置,它给出了以下错误:
While attempting to create applicationformuserform(alias: Application\Form
\UserForm) an invalid factory was registered for this instance type.
UserFormFactory.php
<?php
namespace Application\Factory\Form;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Application\Form\UserForm;
class UserFormFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $serviceLocator) {
$services = $serviceLocator->getServiceLocator();
$entityManager = $services->get('Doctrine\ORM\EntityManager');
$form = new UserForm($entityManager);
return $form;
}
}
?>
UserForm.php
<?php
namespace Application\Form;
use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;
use Doctrine\ORM\EntityManager;
class UserForm extends Form implements InputFilterProviderInterface {
protected $entityManager;
public function __construct(EntityManager $entityManager) {
parent::__construct();
$this->entityManager = $entityManager;
}
public function init() {
$this->add(array(
'name' => 'username',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'User Name',
),
));
$this->add(array(
'name' => 'first_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'First Name',
),
));
$this->add(array(
'name' => 'last_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Last Name',
),
));
$this->add(array(
'name' => 'role_id',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => array(
'object_manager' => $this->entityManager,
'target_class' => 'Application\Entity\Role',
'property' => 'id',
'is_method' => true,
'find_method' => array(
'name' => 'getRoles',
),
'label' => 'User Role',
),
));
}
public function getInputFilterSpecification() {
return array(); // filter and validation here
}
}
?>
Module.config.php
'form_elements' => array(
'factories' => array(
'Application\Form\UserForm' => 'Application\Factory\Form\UserFormFactory',
),
),
我正在另一个控制器工厂中使用这个表单工厂
UserControllerFactory.php
<?php
namespace Member\Factory\Controller;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Member\Controller\UserController;
use Application\Form\UserForm;
class UserControllerFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $serviceLocator) {
$services = $serviceLocator->getServiceLocator();
$userForm = $services->get('FormElementManager')->get('Application\Form\UserForm');
$controller = new UserController($userForm);
return $controller;
}
}
?>
谁能告诉我可能是什么问题?
看了一遍又一遍的代码,我自己找到了答案。实际上我的 Factory 和 Form 文件夹在 src 文件夹之外,这就是 Zend 找不到所有 类 两个文件夹。
我移动了 src 中的 Factory 和 Form 文件夹,现在它工作正常。
我遇到了类似的问题。我对我的工厂 classes 进行了一些更改(重构 + 较小的 class 名称更改)。结果是因为我使用的是 Classmap Autoloader ...而忘记在模块结构中重新 运行 php vendor/bin/classmap_generator.php ...新重命名的 class没有找到。可惜没有生成 "class not found" 错误。
未找到您的工厂。
在其他答案中检查您是否在控制器中使用 PSR-4 或 PSR-0
简要
- 您是否正确命名了工厂(没有拼写错误)?
- 您的 composer.json 是否使用模块的 PSR-0 或 PSR-4 命名空间更新?
- 你 运行
composer dump-autoload
了吗? - 您的
autoload_classmap.php
是否包含过时的条目并混淆了自动加载器? - 检查您的文件夹结构和名称
- 确保你的工厂
implements FactoryInterface
问问自己 "Why is my Factory class not being found when I have placed it right there" 显然必须在哪里找到它?这将帮助您找到问题所在。