Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use view helpers in controllers in Zend Framework

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) ?

like image 836
Matthieu Napoli Avatar asked Mar 08 '10 12:03

Matthieu Napoli


2 Answers

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.

like image 197
Gordon Avatar answered Nov 06 '22 23:11

Gordon


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!

like image 32
kindaian Avatar answered Nov 07 '22 00:11

kindaian