5

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

?>

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

?>

何が問題なのか誰か教えてもらえますか?

4

3 に答える 3

4

コードを何度も見た後、自分で答えを得ました。実際、私のFactoryフォルダーとFormフォルダーはsrcフォルダーの外にあったため、Zend は両方のフォルダーのすべてのクラスを見つけることができませんでした。

Factory と Form フォルダーの両方を src に移動したところ、正常に動作するようになりました。

于 2015-03-20T02:48:38.570 に答える
1

同様の問題がありました。ファクトリ クラスにいくつかの変更を加えました (リファクタリング + マイナーなクラス名の変更)。Classmap Autoloader を使用していたため、モジュール構造で php vendor/bin/classmap_generator.php を再実行するのを忘れていたため、新しく名前が変更されたクラスが見つかりませんでした。残念ながら、「クラスが見つかりません」というエラーは生成されませんでした。

于 2015-07-27T10:03:30.010 に答える