I specified list of zend components in my composer.json

"zendframework/zend-xmlrpc": "2.0.*",
"zendframework/zend-config": "2.0.*",
"zendframework/zend-log": "2.0.*",
"zendframework/zend-db": "2.0.*",
"zendframework/zend-inputfilter": "2.0.*",
"zendframework/zend-json": "2.0.*",
"zendframework/zend-form": "2.0.*",
"zendframework/zend-mvc" :  "2.0.*",
"zendframework/zend-session" :  "2.0.*",
"zendframework/zend-view" :  "2.0.*"

But when I try to output form inside view script:

<?php

   use Zend\Form\Form;
   use Zend\Form\Element;

   $form = new Form();

   $form->setAttribute('action', '/contact/process'); 
   $form->setAttribute('method', 'post');

   $form->prepare();
   echo $this->form()->openTag($form);

   echo $this->form()->closeTag();
   ?>

I get exception:

ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for form

#in /var/www/projectdir/vendor/zendframework/zend-servicemanager/Zend/ServiceManager/ServiceManager.php line 452
#at ServiceManager->get('form', true) in /var/www/projectdir/vendor/zendframework/zend-servicemanager/Zend/ServiceManager/AbstractPluginManager.php line 110
#at AbstractPluginManager->get('form', null) in  /var/www/projectdir/vendor/zendframework/zend-view/Zend/View/Renderer/PhpRenderer.php line 340
#at PhpRenderer->plugin('form') in /var/www/projectdir/vendor/zendframework/zend-view/Zend/View/Renderer/PhpRenderer.php line 359
#at PhpRenderer->__call('form', array()) in /var/www/projectdir/views/editWorker.phtml line 19
#at PhpRenderer->form() in /var/www/projectdir/views/editWorker.phtml line 19
.....

But other views helpers such as $this->htmlList($items) work properly

有帮助吗?

解决方案

The form view helpers are all located in the Zend\Form\View\Helper namespace. There is a separate config where all form view helpers are injected in the view helper plugin manager.

Usually this happens automatically. At least, when you require "zendframework/zendframework" and not all individual components this works out of the box. In the Zend\Mvc namespace there is a special factory which helps to instantiate the view helper's plugin manager. It also tries to inject view helpers from other components: the navigation, i18n and form view helpers.

That being said, there are either two options where this went wrong for you:

  1. The factory does a class_exists() call on those additional view helper configs. If the class Zend\Form\View\HelperConfig cannot be found due to autoloading issues, the form view helpers are not initialized.

  2. The factory is not even called. If you use Zend\Mvc\Application this can be a bug and please provide some more info about your case so others can reproduce it. If you do not use the Application, you have to wire these kind of things manually.

In the case of the latter and you want to wire the stuff yourself, instantiate the helper config, grab the view helper manager and inject it:

// $renderer is the Zend\View\Renderer\PhpRenderer

$plugins = $renderer->getHelperPluginManager();
$config  = new Zend\Form\View\HelperConfig;
$config->configureServiceManager($plugins);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top