简体   繁体   English

如何在我的PHP应用程序中将ZF2表单用作独立模块?

[英]How can I use ZF2 forms as a stand-alone module in my PHP application?

Looking at any kind of basic example on ZF2 forms, I have something like this: 看看ZF2表单上的任何基本示例,我都有类似这样的内容:

In Controller (any basic tutorial example) 在Controller中 (任何基本教程示例)

    $username = new Element\Text('username');
    $username->setLabel('Username')->setAttributes(array(
        'class' => 'username',
        'size' => '30'
    ));

    $form = new Form('my-form');
    $form->add($username);
    return $this->partial("xxx.phtml", array(
        'form' => $form
    ));

In View 视野中

/**
 * inside view template
 *
 * @var \Zend\View\Renderer\PhpRenderer $this
 */
echo $this->form($this->form);

Error I get 我得到错误

 PHP Fatal error: Uncaught exception 'Zend\\ServiceManager\\Exception\\ServiceNotFoundException' with message 'Zend\\View\\HelperPluginManager::get was unable to fetch or create an instance for form' in vendor\\zendframework\\zend-servicemanager\\src\\ServiceManager.php:557 Stack trace: #0 vendor\\zendframework\\zend-servicemanager\\src\\AbstractPluginManager.php(161): Zend\\ServiceManager\\ServiceManager->get('form', true) #1 vendor\\zendframework\\zend-view\\src\\Renderer\\PhpRenderer.php(372): Zend\\ServiceManager\\AbstractPluginManager->get('form', NULL) #2 vendor\\zendframework\\zend-view\\src\\Renderer\\PhpRenderer.php(390): Zend\\View\\Renderer\\PhpRenderer->plugin('form') #3 module\\XXX\\view\\xxx\\xxx.phtml(8): Zend\\View\\Renderer in vendor\\zendframework\\zend-servicemanager\\src\\ServiceManager.php on line 557 

I suspect that some fundamental plugin is not set up. 我怀疑没有设置一些基本的插件。 Perhaps even the service that must register the plugin is not set up (ServiceManager) 也许甚至必须注册插件的服务也未设置(ServiceManager)

What can I do to get ZF2 forms working in my otherwisely non-ZF2 app? 如何使ZF2表单在原本非ZF2的应用程序中运行?

My intent is to use ZF2 form component to build forms, and to use them in my php application. 我的意图是使用ZF2表单组件来构建表单,并在我的php应用程序中使用它们。

As you are not using the full application, the ViewPluginManager will not be provided with the names of the form view helpers by default; 由于您没有使用完整的应用程序,因此ViewPluginManager情况下不会为ViewPluginManager提供表单视图助手的名称。 as they live the Zend\\Form\\View namespace. 他们居住在Zend\\Form\\View命名空间中。

You can easily register the missing services with a few lines best placed in your 'render factory' if you have one. 您可以轻松注册丢失的服务,如果有的话,可以在“渲染工厂”中最好放置几行。

$viewPluginManager = $render->getHelperPluginManager();

$formConfig = new \Zend\Form\View\HelperConfig();
$formConfig->configureServiceManager($viewPluginManager);

echo $render->form($this->form);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM