Calling another Action Helper from another action Helper in Zend Framework - zend-framework

Is it possible to call another action helper from another action helper. Actually I have created an action helper which preform some layout related stuff using preDispatch hook. Now I want to call ajaxContext within my first action helper's preDispatch method. any ideas how can I achieve this?

You can use the Zend_Controller_Action_HelperBroker to fetch other helpers.

If you extend from Zend's abstract class you can use actionController variable to access controller and use sth. like $this->_actionController->layout(); (for layout action helper)

Related

Is it necessary to have a view file with every controller action

Whenever I create a new action in the zend framework controller using the zf CLI tool it creates the corresponding view file. However, I don't need the view file for every action in the controller.
If I delete that view file manually will it affect my project.xml file or have any other effect on my project ?
If your action does not require a view then you can disable it:-
public function myactionAction()
{
$this->_helper->layout()->disableLayout();//to disable layout
$this->_helper->viewRenderer->setNoRender(true);//to disable view
}
If you want to disable the view/layout for the whole controller then you can put the lines above in the init() method of your controller like this:-
public function init()
{
$this->_helper->layout()->disableLayout();//to disable layout
$this->_helper->viewRenderer->setNoRender(true);//to disable view
}
Once you have done that you can safely delete the view files without affecting anything else.
More details are available in the Action Controller manual.
No you don't need to.
Been a while since i worked with Zend Framework. But if memory serves me well, you have two options here.
1 - $this->_helper->viewRenderer->setNoRender(true);
Which will stop the view being rendered
2- You can simply do what you need to do and call exit() in the end of the of your action.
Hope it helps.
Cheers

Accessing ViewHelper from another ViewHelper specifically FlashMessenger

So I created a view helper to specialize/simplify some of the functionality of the Flash Messenger helper.
I have been looking around a lot and saw that you should be able to access the FlashMessenger helper through code like this:
$this->view->_helper->flashMessenger->getMessages();
This won't work for me. I can access view but not anything under _helper. My helper class does extend Zend_View_Helper_Abstract. Do you have to explicitly pass in the view from the view script?
The $_helper property of Zend_Controller_Action stores an instance of the Zend_Controller_Action_HelperBroker, so instead of passing it from the view to your helper, you can get an instance of the HelperBroker and then get the FlashMessenger object from there.
// inside your view helper
$messenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
Now you have access to the Flash Messenger from within your view helper. You can do the same from anywhere in the application to get a reference to any Action Helpers.
Also note, if the helper has not yet been created, calling getStaticHelper will initialize it for you.
See Action Helpers - The Helper Broker for more information.

Is there a way to disable a view helper inside the controller/action?

I have my view helpers in the layout like:
$this->viewSearchForm();
that is ok, in all the pages is show it, but what if a have two or tree page where i don't want to show that view helper? is this possible?
something like in an action:
$this->view->disable('viewSearchForm');
You could pass a flag to your view from your controller (init method or specific action).
In your layout you can have something like
if (!isset($this->disableSearchForm)) {
echo $this->view->viewSearchForm();
}
and from your controller send the following
$this->view->disableSearchForm = true;
I think you can't "disable" a view helper. If the helpers you have are in a specific folder, a workaround could be to remove that folder from the helper path using setHelperPath(), but the default view helpers path is never overwritten. See the Zend_View_Helper documentation for details.
Hope that helps,

Zend passing data from action in controller to view in another controler

How can I pass data from action in controller to view in another controller?
From action in controler to view in the same controller is easy:
I simply write in action's function :
$this->view->assign('error', 'Wrong login');
and in view I recieve it in this way:
<?=$this->escape($this->error);?>
but how can I do it to receive it in view of another controller?
I might be wrong but my guess is that for every request there is only one Zend_View, so if you set something on ControllerA and forward execution to ControllerB you could access that data in the same way.
It´ll not work if you use the action helper _redirect because it´s a browser redirection, to just forward execution to another "place" use the _forward helper instead.
Another option is the flashMessenger helper, that a look at the docs
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html

How to disable a view script in a controller - Zend Framework

I am playing with zend framework's MVC. One thing I found out is that Zend Framework tries to include a view script whenever a controller is called. I can disable it in bootstrap with the following code.
$frontController->setParam('noViewRenderer',true);
However, I have to initialize Zend_View class in a controller method then render a script file myself.
How can I stop including a view script in a controller method so I can disable it if only I want to?
you can disable the view renderer controller helper, with this code in your controller:
public function myAction()
{
$this->_helper->viewRenderer->setNoRender(true);
// from now on, ZF won't search for a matching view script file.
}
The best example would be to use both commands above:
public function myAction() {
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
}
First one disables layout,in general is enabled
application.ini
default
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
$this->_helper->layout->disableLayout();
and second disables view script (.phtml) so exception is not thrown if view script file is not found
$this->_helper->viewRenderer->setNoRender(true);
There are also view helpers that include bouth listened abowe and are not necessary, for example you want to return JSON from array data and not render view element.
public function myAction() {
$this->_helper->json(array());
}
will not render layout nor view script.
Easy, just disable it from within your action.
$this->_helper->layout->disableLayout();
If you aren't talking about layouts, then just add an exit() to your action. Just understand what sort of impact that will have on your application.