【问题标题】:Recommended Path For Zend Form Element View ScriptsZend 表单元素视图脚本的推荐路径
【发布时间】:2011-01-13 05:50:36
【问题描述】:

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

谢谢!

【问题讨论】:

  • 你真的需要调用你的控制器'form'吗?如果你最终得到两个表单控制器怎么办?
  • 控制器名称必须是唯一的,所以我不听你的问题。不过,问题更多是关于放置视图脚本的最佳位置。不管控制器名称如何,我认为我没有将它们放在最佳位置。

标签: zend-framework zend-form zend-form-element


【解决方案1】:

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

/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 中查找。

【讨论】:

  • 为什么在视图脚本文件名中使用前导下划线?
  • 因为它是部分脚本的 ZF 命名约定……它不是特别重要,但我喜欢尽可能地遵循命名约定。
猜你喜欢
  • 1970-01-01
  • 2010-12-22
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 2011-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多