Zend Form Element View 脚本的推荐路径

2024-02-13

我已经开始将表单元素视图脚本放在“/application/views/scripts/form/”下,并能够通过“form/scriptname.phtml”引用它们,但现在我需要制作一个“表单”控制器,我意识到这是一个短视的解决方案。我见过的示例使用类似“/path/to/your/view/scripts/”之类的内容,这并不能帮助我了解放置它们的逻辑/推荐位置。

Thanks!


我使用非标准文件结构并为我的应用程序使用模块:

/application
  /default
    /controllers
      /IndexController
      /ErrorController
    /views
      /scripts
        /index
          /index.phtml
        /error
          /error.phtml
/configs
  /config.ini
/library
  /Zend
/views
  /layouts
    /default.phtml
  /scripts
    /form
      /_text.phtml

为此,您必须在 Zend_Application 的配置中添加模块目录:

[production]

phpsettings.display_startup_errors = 0
phpsettings.display_errors = 0
resources.layout.layout = "default"
resources.layout.layoutpath = "c:\xampp\files\views\layouts"
resources.frontcontroller.moduledirectory = "c:\xampp\files\application"

[development : production]

phpsettings.display_startup_errors = 1
phpsettings.display_errors = 1

视图脚本路径按 LIFO 顺序加载。假设您没有添加任何其他脚本路径,您可以通过以下方式在操作控制器 init() 方法中添加脚本路径:

<?php

class IndexController extends Zend_Controller_Action {

  public function init() {

    $appScriptPath = 'c:\xampp\files\views\scripts';
    $modScriptPath = array_shift($this->view->getScriptPaths());

    $this->view->setScriptPath(NULL);

    $this->view->addScriptPath($appScriptPath);
    $this->view->addScriptPath($modScriptPath);

  }

  public function indexAction() {

    $form = new Zend_Form();

    $form->addElement(new Zend_Form_Element_Text('text'));
    $form->text->setLabel('Text');
    $options = array('viewScript' => 'form/_text.phtml');
    $decorators = array(array('ViewScript', $options));
    $form->text->setDecorators($decorators);

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

  }

}

系统将首先在控制器视图脚本中查找 viewScript,如果没有找到,它将在 /application/views/scripts 中查找。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Zend Form Element View 脚本的推荐路径 的相关文章

随机推荐