ZF2 An Invalid Factory Was Registered

≡放荡痞女 提交于 2019-12-23 10:01:07

问题


I've the following classes and my module config in ZF2 application and it is giving the below error:

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

?>

Module.config.php

'form_elements' => array(
            'factories' => array(
                    'Application\Form\UserForm' => 'Application\Factory\Form\UserFormFactory',
            ),
    ),

And I'm using this form factory in another controller factory

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

?>

Could anybody tell me that what may be the issue?


回答1:


Your Factory is not being found.

Check if you using PSR-4 or PSR-0 in your controller among the other answers

Briefly

  • did you name the Factory correctly (no misspellings)?
  • Is your composer.json updated with PSR-0, or PSR-4 namespaces for your modules?
  • did you run composer dump-autoload?
  • Does your autoload_classmap.php contain an outdated entry & confuses autoloader?
  • Check your folder structure and names
  • make sure your Factory implements FactoryInterface

Ask yourself "Why is my Factory class not being found when I have placed it right there" where it obviously without a doubt must be found? That will help you guide your way into finding out what's wrong.




回答2:


I got my answer at my own after looking at the code again and again. Actually my Factory and Form folders were outside of src folder that's why Zend could not found all the classes of both folders.

I moved both Factory and Form folder in src and now it's working fine.




回答3:


I had a similar problem. I had made some changes to my factory classes (refactoring + minor class name changes). Turns out that because I was using the Classmap Autoloader ... and forgot to re-run php vendor/bin/classmap_generator.php in the module structure ... the newly renamed classes were not found. Too bad a "class not found" error wasn't generated.



来源:https://stackoverflow.com/questions/29147690/zf2-an-invalid-factory-was-registered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!