ZF2注册了无效的工厂

Man*_*gir 5 zend-framework-modules zend-framework2 zend-form2

我在ZF2应用程序中拥有以下类和模块配置,并且出现以下错误:

While attempting to create applicationformuserform(alias: Application\Form
\UserForm) an invalid factory was registered for this instance type.
Run Code Online (Sandbox Code Playgroud)

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;
    }
}

?>
Run Code Online (Sandbox Code Playgroud)

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
    }
}

?>
Run Code Online (Sandbox Code Playgroud)

Module.config.php

'form_elements' => array(
            'factories' => array(
                    'Application\Form\UserForm' => 'Application\Factory\Form\UserFormFactory',
            ),
    ),
Run Code Online (Sandbox Code Playgroud)

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

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;
    }
}

?>
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我这可能是什么问题吗?

den*_*smv 6

找不到您的工厂。

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

简要地

  • 您是否正确命名了Factory(没有拼写错误)?
  • 您的composer.json是否已使用模块的PSR-0或PSR-4命名空间更新?
  • 你跑了composer dump-autoload吗?
  • 您是否autoload_classmap.php包含过时的条目并混淆自动装带器?
  • 检查您的文件夹结构和名称
  • 确保你的工厂 implements FactoryInterface

问问自己“为什么在我将其放在此处时找不到我的Factory类”,显然必须毫无疑问地找到它?这将帮助您指导找出问题的出路。


Man*_*gir 4

一遍又一遍地查看代码后,我自己得到了答案。实际上我的FactoryForm文件夹位于src文件夹之外,这就是 Zend 无法找到这两个文件夹的所有类的原因。

我将 Factory 和 Form 文件夹移到了 src 中,现在工作正常。