加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 21:53:22 所属栏目:PHP教程 来源:网络整理
导读:我在ZF2应用程序中有以下类和我的模块配置,它给出了以下错误: While attempting to create applicationformuserform(alias: ApplicationFormUserForm) an invalid factory was registered for this instance type. UserFormFactory.php ?phpnamespace App
我在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','options' => array(
                        'label' => 'First Name',));
        $this->add(array(
                'name' => 'last_name','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是否包含过时的条目&混淆自动加载器?
>检查文件夹结构和名称
>确保您的Factory实现FactoryInterface

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读