duangua6912
2016-02-22 22:51
浏览 42
已采纳

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

Looking at any kind of basic example on ZF2 forms, I have something like this:

In Controller (any basic tutorial example)

    $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)

What can I do to get ZF2 forms working in my otherwisely non-ZF2 app?

My intent is to use ZF2 form component to build forms, and to use them in my php application.

图片转代码服务由CSDN问答提供 功能建议

查看ZF2表单上的任何基本示例,我都有类似的内容: \ n

在控制器(任何基本教程示例)

  $ username = new Element \ Text('username'); 
 $ username  - > setLabel('Username') - > setAttributes(array(
'class'=>'username',
'custity'=> '30'
)); 
 
 $ form  = new Form('my-form'); 
 $ form-> add($ username); 
 return $ this-> partial(“xxx.phtml”,array(
'form'=>  $ form 
)); 
   
 
 

在视图中

  / **  
 *内部视图模板
 * 
 * @var \ Zend \ View \ Renderer \ PhpRenderer $ this 
 * / 
echo $ this-> form($ this-> form); 
   
 
 

我遇到错误

  PHP致命错误:未捕获的异常'Zend \ 带有消息'Zend \ View \ HelperPluginManager :: get的ServiceManager \ Exception \ ServiceNotFoundException'无法获取或创建 表单'在vendor \ zendframework \ zend-servicemanager \ src \ ServiceManager.php中的立场:557 
堆栈跟踪:
#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->插件('form')
#3 module \ XXX \ view \ xxx  \ xxx.phtml(8):第557行\ _x   
  
 
中的供应商\ zendframework \ zend-servicemanager \ src \ ServiceManager.php中的Zend \ View \ Renderer  

我怀疑没有设置一些基本插件。 甚至可能还没有设置必须注册插件的服务(ServiceManager)

如何让ZF2表单在我的非ZF2应用程序中运行?

我的目的是使用ZF2表单组件来构建表单,并在我的php应用程序中使用它们。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtgv52982 2016-02-23 03:28
    已采纳

    As you are not using the full application, the ViewPluginManager will not be provided with the names of the form view helpers by default; as they live the Zend\Form\View namespace.

    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);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题