从控制器或表单内解析视图助手位置

发布于 2024-10-17 06:09:28 字数 1801 浏览 0 评论 0原文

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

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 中包含 CKEditor
  2. project/project/version 中包含 jQuery UI

当我在视图脚本内时,调用 jQueryUI(); ?> 的工作方式就像一个超级按钮,即使帮助程序位于默认模块的帮助程序目录中。然而,对于控制器和表单来说却并非如此。

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

$this->view->jQueryUI();

,效果是异常:

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

同样,在 AddIssue.php 表单中,我尝试了这:

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

又出现了一个例外:

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

显然,如果我的视图助手位于模块/的助手目录中,则两者都可以工作它们被调用的控制器,但由于它们在许多模块中使用,我希望将它们放在默认模块的视图助手目录中。

所以,我的问题是:

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

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

What I want to do:

  1. include CKEditor in the view project/project/issueadd
  2. include jQuery UI in project/project/version

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.

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

$this->view->jQueryUI();

and the effect was an exception:

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/

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

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

and there was an exception again:

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/

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.

So, my questions are:

  1. How do I access those view helpers from within the controller and the form?
  2. Is there a simpler way to get around this (apart from simply including all javascript files in the layout)? Like creating a plugin or an action helper? (I haven't done these things before, so I really don't know, I'm only starting my adventure with ZF).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦与时光遇 2024-10-24 06:09:28

关于 Q1(基于评论)。您应该能够以通常的方式访问帮助程序。但是,由于它不起作用,我认为引导视图资源的方式和/或执行帮助程序的具体注册方式或向其添加帮助程序路径的方式存在问题。我在 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 的模块化设置。

关于第二季度:
您可以使用 headScript 查看助手来管理在布局的 head 标记中加载哪些脚本。使用这个助手,你可以通过你的行动来做到这一点。

例如。如果在layout.php中你有:

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

那么在例如indexAction中你可以附加一些JS文件,如下所示:

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

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.
        );
    }

}
?>

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

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.

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

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

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

$this->view->headScript()->appendFile($this->view->baseUrl('/js/someJS.js'));
她比我温柔 2024-10-24 06:09:28

尽管我讨厌回答自己的问题,但根据 Marcin 在他的回答中提出的建议,我还提出了一个解决方案。也可以在 application.ini 中完成:

resources.view[] =
resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/modules/default/views/helpers"

需要注意的是这些行需要按此顺序出现。如果相反,resources.view[] = 之前的任何内容都将被忽略。

As much as I hate answering my own questions, there's one more solution I came up with, based on what Marcin has suggested in his answer. It can also be done in application.ini:

resources.view[] =
resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/modules/default/views/helpers"

The caveat is that the lines need to appear in this order. Should it be reversed, anything before resources.view[] = will be ignored.

亢潮 2024-10-24 06:09:28

我宁愿摆脱你的 JQueryUI.php 并使用 ZendX。类似的东西:

在控制器中:

    ZendX_JQuery::enableView ($this->view);
    $this->view->jQuery ()->enable ()->setRenderMode (ZendX_JQuery::RENDER_ALL);

在布局中:

<?php echo $this->jQuery () ?>

I'd rather get rid of your JQueryUI.php and would use ZendX. Something like that:

In controller:

    ZendX_JQuery::enableView ($this->view);
    $this->view->jQuery ()->enable ()->setRenderMode (ZendX_JQuery::RENDER_ALL);

In layout:

<?php echo $this->jQuery () ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文