Zend-Framework View Helper - 是否与特定视图相关?
当我们在 Zend 应用程序上创建视图助手时,该助手是否可用于所有视图,或者我们是否应该以某种方式告知该视图助手可用于特定视图? 如果在视图文件夹“something”上,我们有多个文件怎么办?这些文件中的任何一个都可以调用它吗?
多谢, MEM
When we create a view helper, on a Zend application, will that helper be available for ALL the views or, should we somehow tell that THAT view helper is available to a specific view?
What if, on the view folder "something", we have more then one file ? Any of those files can call it?
Thanks a lot,
MEM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用视图助手时,框架将在通过 $view->addHelperPath() 定义的路径中查找。通常,这样的调用将包含伪命名空间以及路径:
$view->addHelperPath('My/View/Helper', 'My_View_Helper_');
然后当您调用视图时布局或视图脚本中的帮助程序:
someHelper() ?>
框架将执行 LIFO 搜索,附加前缀(在上述情况下:< code>'My_View_Helper_') 到类名
'SomeHelper'
,然后尝试加载由addHelperPath()
映射定义的文件。在默认设置中,框架通过调用
以下方式预加载 Zend 视图助手:
$view->addHelperPath('Zend/View/Helper', 'Zend_View_Helper_');
这就是您可以使用的原因所有 Zend 提供的开箱即用的视图助手。
由于所有这些处理都独立于哪个视图脚本进行调用,因此它可以在任何视图脚本中工作。 [实际上存在一些与调用其他模块中定义的视图助手相关的问题,但这是一个单独的问题。]
When you call a view helper, the framework will look within the paths defined via
$view->addHelperPath()
. Typically, such a call will include a pseudo-namespace as well as a path:$view->addHelperPath('My/View/Helper', 'My_View_Helper_');
Then when you call a view helper in a layout or a view script:
<?php echo $this->someHelper() ?>
The framework will do a LIFO search, appending the prefixes (in the above case:
'My_View_Helper_'
) to the classname'SomeHelper'
and then attempting to load the file defined by theaddHelperPath()
mapping.In the default setup, the framework pre-loads the Zend view helpers by calling:
$view->addHelperPath('Zend/View/Helper', 'Zend_View_Helper_');
which is why you can use all the Zend-provided view helpers right out of the box.
Since all this processing is independent of which view script is making the call, it will work in any view script. [There are actually some issues associated to calling view helpers defined in other modules, but that's a separate issue.]