19

I have a controller that is called with AJAX (sends JSON data), so I don't use a view.

I need to use a personnal view helper to 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) ?

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261

5 Answers5

45

You can access any ViewHelper from the Controller by

$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.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Yes how could I have forget that ! I feel stupid, but thank you. For the "is that right doing so" question : the view helper is there to format dates, numbers... How could I do differently ? (I can't do that with Javascript) – Matthieu Napoli Mar 08 '10 at 16:29
  • @Matthieu Ah, I see. I'd say ViewHelper sounds ok in this case then. – Gordon Mar 08 '10 at 16:38
  • i tried using the 3rd option but i get Call to undefined method Zend_Controller_Action_Helper_ViewRenderer::getView() , i am in a custom class being called by the controller – max4ever Dec 05 '11 at 11:10
  • 2
    i found out how, in boostrap have _initView that returns $view, then use this $view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view'); – max4ever Dec 05 '11 at 14:30
5
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!

coolguy
  • 7,866
  • 9
  • 45
  • 71
kindaian
  • 51
  • 1
  • 1
3

You can create an instance of a Helper .. this will work in Controllers, Models and everywhere you need the Helper.

eg.

// create Instance   
$serverUrl_helper = new Zend_View_Helper_ServerUrl();

// get the ServerUrl
$serverUrl = $serverUrl_helper->serverUrl();
Danilo
  • 47
  • 6
1

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.

David Weinraub
  • 14,144
  • 4
  • 42
  • 64
0

Just use action helpers, many of view helpers are available as action helpers too.

Or directly by using Zend_Date or sprintf.

takeshin
  • 49,108
  • 32
  • 120
  • 164
  • I said "I need to use a *personnal* view helper" – Matthieu Napoli Mar 09 '10 at 12:08
  • Yes but I care about the Zend Framework architecture and I didn't spend months understanding it and creating view helper to tear it away by overriding the "helper" concept and call classes directly, or even worse : sprintf to format my numbers and dates. – Matthieu Napoli Mar 10 '10 at 11:26
  • You also said, `I don't use a view`. You meant you don't use `Zend_View`, or `.phtml` view scripts? $this->view->_helper; is obvious… Who knows what `personnal view helper` is :) – takeshin Mar 10 '10 at 18:54