从控制器或表单中解析视图助手位置 [英] Resolve view helper location from within the controller or form

查看:76
本文介绍了从控制器或表单中解析视图助手位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些视图帮助器,可以在需要时添加JavaScript文件(例如,仅表单使用CKEditor等).我的目录结构(简化为仅包含相关文件)是这样的:

I have a few view helpers that add JavaScript files when they're needed (for instance, so that only forms use CKEditor and such). My directory structure (simplified to include only the relevant files) is this:

application
    --forms
        --Project
            AddIssue.php
    --modules
        --default
            --views
                --helpers
                    JQueryUI.php
                    Wysiwyg.php
        --project
            --controllers
                ProjectController.php
            --views
                --scripts
                    --project
                        version.phtml
                        issueadd.phtml

我想做什么:

  1. 在视图project/project/issueadd
  2. 中包括CKEditor
  3. project/project/version
  4. 中包括jQuery UI
  1. include CKEditor in the view project/project/issueadd
  2. include jQuery UI in project/project/version

当我进入视图脚本时,即使辅助程序位于默认模块的辅助程序目录中,调用<?php $this->jQueryUI(); ?>的操作也像超级按钮一样.但是,对于控制器和表单,情况并非如此.

When I'm inside the view script, calling <?php $this->jQueryUI(); ?> works like a charm, even though the helper is in the default module's helpers directory. However, the same is not true for the controller and the form.

在控制器ProjectController.phpversionAction()中,我尝试调用:

In the controller ProjectController.php, versionAction(), I tried to call:

$this->view->jQueryUI();

效果是一个例外:

消息:在注册表中找不到名为"JQueryUI"的插件;使用的路径:Project_View_Helper_:C:/xampp/htdocs/bugraid/application/modules/project/views \ helpers/Zend_View_Helper_:Zend/View/Helper/

Message: Plugin by name 'JQueryUI' was not found in the registry; used paths: Project_View_Helper_: C:/xampp/htdocs/bugraid/application/modules/project/views\helpers/ Zend_View_Helper_: Zend/View/Helper/

类似地,我以AddIssue.php形式尝试了此操作:

Similarly, in the AddIssue.php form, I tried this:

$this->getView()->wysiwyg();

又有一个例外:

消息:在注册表中找不到名为"Wysiwyg"的插件;使用的路径:Project_View_Helper_:C:/xampp/htdocs/bugraid/application/modules/project/views \ helpers/Zend_View_Helper_:Zend/View/Helper/

Message: Plugin by name 'Wysiwyg' was not found in the registry; used paths: Project_View_Helper_: C:/xampp/htdocs/bugraid/application/modules/project/views\helpers/ Zend_View_Helper_: Zend/View/Helper/

很显然,如果我的视图助手位于被调用的模块/控制器的helper目录中,则两者都可以使用,但是由于它们已在许多模块中使用,因此我希望将它们放在默认模块的查看助手目录.

Obviously, both would work if my view helpers were in the helper directories of the modules/controllers they're being called from, but since they're used across many modules, I'd like to have them in the default module's view helpers directory.

所以,我的问题是:

  1. 如何从控制器和表单中访问这些视图助手?
  2. 是否有更简单的方法来解决此问题(除了在布局中仅包含所有javascript文件之外)?喜欢创建插件或动作助手? (我以前没有做过这些事情,所以我真的不知道,我只是从ZF开始我的冒险之旅.)

推荐答案

关于Q1(基于注释).您应该能够以通常的方式访问帮助程序.但是,由于它不起作用,因此我认为引导资源的方式和/或如何对助手进行具体注册或向其中添加助手路径的方式存在问题.我在Bootsrap.php中粘贴了一个添加帮助程序路径的示例:

Regarding Q1 (based on the comments). You should be able to access the helpers in a usual way. However since it does not work, I think there is a problem with the way you bootstrap your view resource and/or the way how you perform concrete registration of the helpers or how you add helper path to it. I paste an example of adding helper path in Bootsrap.php:

<?php
#file: APPLICATION_PATH/Bootstrapt.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    public function _initViewHelperPath() {

        $this->bootstrap('view');
        $view = $this->getResource('view');

          $view->addHelperPath(
                APPLICATION_PATH . '/modules/default/views/helpers',
                'My_View_Helper' // <- this should be your helper class prefix.
        );
    }

}
?>

这种偏离路线通常应适用于ZF的模块化设置.

This off course should normally work for modular setup of ZF.

关于第二季度: 您可以使用 headScript 查看帮助程序,以管理要在布局的head标记中加载哪些脚本.使用此帮助程序,您可以从操作中完成操作.

Regarding Q2: You can use headScript view helper to manage what scripts do you load in the head tag of your layout. Using this helper you can do it from your actions.

例如.如果在layout.php中,您将:

For example. If in a layout.php you have:

<head>
    <?php echo $this->headScript(); ?>
</head>

然后,例如indexAction可以添加一些JS文件,如下所示:

then in, e.g. indexAction you can append some JS file as follows:

$this->view->headScript()->appendFile($this->view->baseUrl('/js/someJS.js'));

这篇关于从控制器或表单中解析视图助手位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆