ZF2注册了一个无效的工厂



我在ZF2应用程序中有以下类和模块配置,它给出了以下错误:

While attempting to create applicationformuserform(alias: ApplicationForm
UserForm) an invalid factory was registered for this instance type.

UserFormFactory.php

<?php
namespace ApplicationFactoryForm;
use ZendServiceManagerFactoryInterface;
use ZendServiceManagerServiceLocatorInterface;
use ApplicationFormUserForm;
class UserFormFactory implements FactoryInterface {
    public function createService(ServiceLocatorInterface $serviceLocator) {
        $services         = $serviceLocator->getServiceLocator();
        $entityManager    = $services->get('DoctrineORMEntityManager');
        $form = new UserForm($entityManager);
        return $form;
    }
}
?>

UserForm.php

<?php
namespace ApplicationForm;
use ZendFormForm;
use ZendInputFilterInputFilterProviderInterface;
use DoctrineORMEntityManager;
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' => 'DoctrineModuleFormElementObjectSelect',
                'options' => array(
                        'object_manager'     => $this->entityManager,
                        'target_class'       => 'ApplicationEntityRole',
                        '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(
                    'ApplicationFormUserForm' => 'ApplicationFactoryFormUserFormFactory',
            ),
    ),

我正在另一家控制器工厂中使用这个表单工厂

UserControllerFactory.php

<?php
namespace MemberFactoryController;
use ZendServiceManagerFactoryInterface;
use ZendServiceManagerServiceLocatorInterface;
use MemberControllerUserController;
use ApplicationFormUserForm;
class UserControllerFactory implements FactoryInterface {
    public function createService(ServiceLocatorInterface $serviceLocator) {
        $services    = $serviceLocator->getServiceLocator();
        $userForm    = $services->get('FormElementManager')->get('ApplicationFormUserForm');
        $controller  = new UserController($userForm);
        return $controller;
    }
}
?>

有人能告诉我可能出了什么问题吗?

找不到您的工厂。

检查您是否在控制器中使用PSR-4或PSR-0以及其他答案

简要介绍

  • 你给工厂起的名字正确吗(没有拼写错误)
  • 您的composer.json是否为模块更新了PSR-0或PSR-4命名空间
  • 你运行composer dump-autoload了吗
  • 您的autoload_classmap.php是否包含过时的条目&混淆了自动加载器
  • 检查文件夹结构和名称
  • 确保您的工厂implements FactoryInterface

问问自己"为什么我的工厂类放在那里却找不到",毫无疑问,它必须在哪里找到?这将帮助你找到问题所在。

在反复查看代码后,我自己得到了答案。事实上,我的FactoryForm文件夹都在src的文件夹之外,这就是为什么Zend找不到这两个文件夹的所有类的原因。

我移动了src中的Factory和Form文件夹,现在它工作得很好。

我也遇到了类似的问题。我对工厂类做了一些更改(重构+小的类名更改)。事实证明,因为我使用的是Classmap自动加载器。。。忘记在模块结构中重新运行phpvendor/bin/classmap_generator.php。。。找不到新重命名的类。糟糕的是,没有生成"找不到类"错误。

最新更新