本文介绍了Zend 表单元素视图脚本的推荐路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I had started putting my form element view scripts under '/application/views/scripts/form/' and was able to reference them by 'form/scriptname.phtml', but now I need to make a 'form' controller and I realize this was a short-sighted solution. The examples I've seen use something like '/path/to/your/view/scripts/' which doesn't help me with what's a logical/recommended place to put them.

谢谢!

推荐答案

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

I use non-standard file structure and use modules for my applications:

/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 的配置中添加模块目录:

To do it this way, you have to add the module directory in your config for 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() 方法中添加您的脚本路径:

View script paths are loaded in LIFO order. Assuming you have not added any other script paths, you can add your script paths in the action controller init() method this way:

<?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 中查找.

The system will look in the controller views scripts for your viewScript first, and then if it does not find it, it will look in /application/views/scripts.

这篇关于Zend 表单元素视图脚本的推荐路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 22:07