php 在 Zend Framework 中的控制器中使用视图助手
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2401392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use view helpers in controllers in Zend Framework
提问by Matthieu Napoli
I have a controller that is called with AJAX (sends JSON data), so I don't use a view.
我有一个用 AJAX 调用的控制器(发送 JSON 数据),所以我不使用视图。
I need to use a personnal view helperto format my data, but in my controller.
我需要使用个人视图助手来格式化我的数据,但在我的控制器中。
Is that possible ?
那可能吗 ?
Or maybe I am doing it wrong(maybe I should have a view, but how with JSON) ?
或者我做错了(也许我应该有一个视图,但是如何使用 JSON)?
回答by Gordon
You can access any ViewHelper from the Controller by
您可以通过以下方式从控制器访问任何 ViewHelper
$this->view->helpername(/*params*/);
// or
$helper = $this->view->getHelper('helpername');
// or
$broker = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$broker->getView()->helpername(/*params*/);
See Zend: How to use a custom function from a view helper in the controller?
However, you might be right that you are doing it wrong (funny pic btw), but I cannot really tell from your question. Please refine it as to why you need to call the view helper and what it is supposed to format.
但是,您做错了可能是对的(顺便说一句有趣的图片),但我无法从您的问题中真正看出。请细化它,说明为什么需要调用视图助手以及它应该格式化什么。
回答by kindaian
Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
Just be sure that the returned view is the view you want. Because down the line, the view may get overwritten and on the controller you have a spank new view.
只要确保返回的视图是您想要的视图。因为接下来,视图可能会被覆盖,并且在控制器上您有一个全新的视图。
And all those values you setup on the view on the action helper and the like... before the controller is kicked in? All gone with the wind!
以及您在操作助手等视图上设置的所有值...在控制器启动之前?一切随风而去!
So test before assuming that if you get a view resource. it is really the same view resource you expect, and that all your vars are still there.
因此,在假设您获得视图资源之前进行测试。它确实与您期望的视图资源相同,并且您的所有变量仍然存在。
You may be surprised as i was!
你可能和我一样惊讶!
回答by Danilo
You can create an instance of a Helper .. this will work in Controllers, Models and everywhere you need the Helper.
你可以创建一个 Helper 的实例.. 这将适用于控制器、模型和任何你需要 Helper 的地方。
eg.
例如。
// create Instance
$serverUrl_helper = new Zend_View_Helper_ServerUrl();
// get the ServerUrl
$serverUrl = $serverUrl_helper->serverUrl();
回答by David Weinraub
Another approach is to use the ContextSwitch or AjaxContext action-helpers. This allows you to use a view-script from which you can then call your view-helper in the standard manner.
另一种方法是使用ContextSwitch 或 AjaxContext action-helpers。这允许您使用视图脚本,然后您可以从该脚本以标准方式调用您的视图助手。
回答by takeshin
Just use action helpers, many of view helpers are available as action helpers too.
只需使用动作助手,许多视图助手也可用作动作助手。
Or directly by using Zend_Dateor sprintf.
或直接使用Zend_Date或sprintf。

