zend-framework2 – ZF2无效工厂已注册

我在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','options' => array(
                        'label' => 'First Name',));
        $this->add(array(
                'name' => 'last_name','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;
    }
}

?>

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

解决方法

你的工厂没有找到.

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

简要地

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

问问自己“当我把它放在那里时,为什么我的工厂课程没有找到”,显然毫无疑问必须找到它?这将帮助您指导您找出错误方法.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...