从控制器或表单内解析视图助手位置
我有一些视图助手,可以在需要时添加 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
我想要做的:
- 在视图
project/project/issueadd
中包含 CKEditor - 在
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/
显然,如果我的视图助手位于模块/的助手目录中,则两者都可以工作它们被调用的控制器,但由于它们在许多模块中使用,我希望将它们放在默认模块的视图助手目录中。
所以,我的问题是:
- 如何从控制器和表单中访问这些视图助手?
- 有没有更简单的方法来解决这个问题(除了简单地在布局中包含所有 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:
- include CKEditor in the view
project/project/issueadd
- 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:
- How do I access those view helpers from within the controller and the form?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关于 Q1(基于评论)。您应该能够以通常的方式访问帮助程序。但是,由于它不起作用,我认为引导视图资源的方式和/或执行帮助程序的具体注册方式或向其添加帮助程序路径的方式存在问题。我在 Bootsrap.php 中粘贴了一个添加辅助路径的示例:
这通常应该适用于 ZF 的模块化设置。
关于第二季度:
您可以使用 headScript 查看助手来管理在布局的
head
标记中加载哪些脚本。使用这个助手,你可以通过你的行动来做到这一点。例如。如果在layout.php中你有:
那么在例如indexAction中你可以附加一些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:
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:
then in, e.g. indexAction you can append some JS file as follows:
尽管我讨厌回答自己的问题,但根据 Marcin 在他的回答中提出的建议,我还提出了一个解决方案。也可以在 application.ini 中完成:
需要注意的是这些行需要按此顺序出现。如果相反,
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:
The caveat is that the lines need to appear in this order. Should it be reversed, anything before
resources.view[] =
will be ignored.我宁愿摆脱你的 JQueryUI.php 并使用 ZendX。类似的东西:
在控制器中:
在布局中:
I'd rather get rid of your JQueryUI.php and would use ZendX. Something like that:
In controller:
In layout: