简体   繁体   English

Zend Framework View Helper-如何使其工作

[英]Zend Framework View Helper - how to get it to work

I'm trying to get started with Zend Framework , followed the quickstart project and am trying to start a new module of my own. 我正在尝试从Zend Framework开始,跟随quickstart项目,并试图开始一个我自己的新模块。 Am trying to implement view helpers and I keep getting the following message: Message: Method formDate does not exist 我正在尝试实现视图助手,并且不断收到以下消息:消息:方法formDate不存在

Last entry at the stack trace: 堆栈跟踪中的最后一个条目:

0 D:\\work\\quickstart_zend\\application\\views\\scripts\\users\\register.phtml(38): Zend_Form_Element->__call('formDate', Array) 0 D:\\ work \\ quickstart_zend \\ application \\ views \\ scripts \\ users \\ register.phtml(38):Zend_Form_Element-> __ call('formDate',Array)

I have the following file structure: 我具有以下文件结构:

quickstart_zend
     + application
        + configs
        + controllers
        [...]
        + views
          + helpers
          + scripts

     [...]
     + library
        + Application
          + Form
            + Element
                Date.php
          + View
            + Helper
                FormDate.php
     + public

I have added in my public/Bootstrap.php this method: 我在我的public / Bootstrap.php中添加了以下方法:

protected function _initActionHelpers()
        {
            Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH.'/../library/Application/View/Helper', 'Application_View_Helper');
            Zend_Controller_Action_HelperBroker::addPrefix('Application_View_Helper');
        }

I have also added in my application.ini: 我还添加了我的application.ini:

autoloaderNamespaces[] = "Application"
    resources.view.helperPath.Application_View_Helper = APPLICATION_PATH "/../library/Application/View/Helper/"

And i have seen a version and also have tried with resources.view.helperPath.Application_View_Helper_, nothing seems to get it to work. 我已经看过一个版本,也尝试过使用resources.view.helperPath.Application_View_Helper_,似乎没有任何效果。

Of course, i have a Users.php form where i create a 'date' element: 当然,我有一个Users.php表单,在其中创建了一个“日期”元素:

// Add a dateOfBirth element
    $element = new Application_Form_Element_Date('dateOfBirth');
    $this->addElement($element);

Of course, i have a Users.php form where i create a 'date' element: 当然,我有一个Users.php表单,在其中创建了一个“日期”元素:

// Add a dateOfBirth element
    $element = new Application_Form_Element_Date('dateOfBirth');
    $this->addElement($element);

And in my view script, where the errors shows up: 在我的视图脚本中,显示错误的位置:

<? echo $form->dateOfBirth->formDate() ?>

What am i missing to get it to work? 我想让它正常工作需要什么? :-( i've spent a day so far looking for solutions :-(到目前为止我已经花了一天时间寻找解决方案

You are receiving this error because there is no such method in Zend_Form_Element. 您收到此错误,因为Zend_Form_Element中没有这样的方法。 I think you are trying to use your view helper to display in some way this form element, but if this is the case it is better to use form decorators. 我认为您正在尝试使用视图帮助器以某种方式显示此表单元素,但是,在这种情况下,最好使用表单装饰器。 You can use the standard decorators or you can create a custom one. 您可以使用标准装饰器,也可以创建自定义装饰器。 Check the documentation for more info - http://framework.zend.com/manual/en/zend.form.decorators.html 查看文档以获取更多信息-http://framework.zend.com/manual/zh/zend.form.decorators.html

to properly use your view helper on that data you would use it like: 要在该数据上正确使用视图帮助器,您将像这样使用它:

In your view (.phtml) 您认为(.phtml)

//a view helper should act on a piece of data and return something
//so I assume your formDate() helper takes a date value and reformats it.
<?php echo $this->formDate($this->form->dateOfBirth) ?>

assuming you assigned your form to the view in your controller using the standard: 假设您已使用标准将表单分配给控制器中的视图:

$this->view->form = $form;

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

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