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

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 Ah, I see. I'd say ViewHelper sounds ok in this case then.
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
i found out how, in boostrap have _initView that returns $view, then use this $view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
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!

Comments

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();

3 Comments

This is true, although not the best solution
@Matthieu, do you have a better way to use helpers in Models?
Do you really use view helpers in Models??
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.

Comments

0

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

Or directly by using Zend_Date or sprintf.

3 Comments

I said "I need to use a personnal view helper"
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.
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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.