zend-framework2 - ZF2 注册无效工厂

标签 zend-framework2 zend-framework-modules zend-form2

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

?>

用户窗体.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
    }
}

?>

模块.config.php
'form_elements' => array(
            'factories' => array(
                    'Application\Form\UserForm' => 'Application\Factory\Form\UserFormFactory',
            ),
    ),

我在另一个 Controller 工厂中使用这个表单工厂

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

?>

谁能告诉我可能是什么问题?

最佳答案

找不到您的工厂。

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

简要地

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

  • 问自己“为什么我的 Factory 类在我把它放在那里时却找不到”,显然毫无疑问必须找到它?这将帮助您引导您找出问题所在。

    关于zend-framework2 - ZF2 注册无效工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29147690/

    相关文章:

    php - 打包通用 zend 模块的最佳方式

    php - 如何在 Zend 框架 2 中为 Zend/Form 生成的标签添加属性

    php - Zend2 Routing __NAMESPACE__ 不起作用或被忽略

    php - 将 Web 应用程序 (ZF2) 转换为 PHAR

    php - 完整性约束违规 - 如何使用类表继承在 Doctrine 中创建实体

    zend-framework - Zend 可重复使用的小部件/插件/迷你应用程序?

    php - 如何在 Zend Framework 2 中为 Hydrator 注册委托(delegate)工厂?

    php - Zend Framework - 重定向到不同模块中的 IndexController

    php - 过滤器和验证器被添加了两次

    php - 在 Zend Framework 2 应用程序的表单集合中使用 dgrid