Accessing ViewHelper from another ViewHelper specifically FlashMessenger - zend-framework

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.

Related

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,

Passing data from controller to master page - based on currently logged in user

Using MVC2
Have a master-page that needs to hide certain menus if currently logged in user does not have correct flags set.
Seems like a common problem. Found examples that require that all controllers inherit from a base controller (I have that) and where in constructor of the base controller, the passing of certain parameters to ViewData can occur. This is great and would be easy for me to do, but User.Identity and Request objects are NULL during the construction of base controller.
How do I get to User.Identity of the currently logged in user so that I can query database & modify the ViewData collection accordingly before Master Page view is rendered?
Thanks
You could use child actions along with the Html.Action and Html.RenderAction helpers. So you could have a controller action which returns a view model indicating the currently logged in user info:
public MenuController: Controller
{
public ActionResult Index()
{
// populate a view model based on the currently logged in user
// User.Identity.Name
MenuViewModel model = ...
return View(model);
}
}
and have a corresponding strongly typed partial view which will render or not the menus. And finally inside the master page include the menus:
<%= Html.Action("Index", "Menu") %>
This way you could have a completely separate view model, repository and controller for the menu. You could still use constructor injection for this controller and everything stays strongly typed. Of course there will be a completely different view model for the main controller based on the current page. You don't need to have base controllers or some base view model that all your action should return.

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

Calling another Action Helper from another action Helper in 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)

How to Notify View Controller that a Property of an Object has Changed

I have a view controller which gets an NSObject conforming to a protocol and then displays a view with the object's properties.
The question : How can my controller know that a property of this object has been modified and then refresh the view ?
Thanks a lot
Thierry
There are three ways of doing this:
Have the object call a method in the controller in response to an event e.g. a user clicking the button. This is usually done using an IBAction.
Set the controller to be the delegate of object e.g. a UIWebView sends a message to its delegate when it finishes loading a page.
Use a notification. The object generates the notification and then one or more objects (including the controller) registers to listen for the notification. This is usually not used with interface elements although it can be.
I can't tell you more without more detail about the specifics of your project.
Your viewcontroller should conform to your .
In your model, all your set methods should trigger appropriate functions you define in your modelchangedprotocol.
This OO design pattern is also known as "Observer" design pattern.