Could someone give few tips and/or examples how Controller Plugins and Action Helpers are different? Are there situations where particular task could be accomplished with one but not another? For me they both look more or less the same and I'm often having trouble having to decide when to use what... Are there any big differences?
Controller plugins can hook into any controller at any point in the routing process (preDispatch postDispatch, routeStartup, routeShutdown) which makes them apt at providing behind the scenes functionality like ACL enforcement.
Action Helpers are for for reusable but optional segments that your controller might need to access (redirector, flashMessenger).
So if you are creating a reusable snippet of code that always needs to execute itself then use a controller plugin, otherwise you probably want an action helper.
You can think of it this way:
Action helpers are used to add methods to controllers.
Controller plugins are used to add routing / dispatch logic to controllers.
So ask yourself, do I have a method that I would like to be able to call from all actions in my controller? Or do I need to add logic to the routing / dispatch process.
You might also have a look at the the Built in Action Helpers.
A picture to illustrate the difference between plugins and action helpers:
ZF Sequence Flow
Action helpers also have access to the actual controlller object that's being executed. Controller Plugins only have access to the FrontController, and therefore only the controller and action name.
Which you use depends on what context you need. If you need to access a view object attached to a controller, for example, you will want an Action Helper.
Also notice that, in the front controller life-cycle process, the plugins get the control(or invoked) first than the action helpers.
Related
I am creating a slightly elaborated form with Angular. This form can be submitted, then modified or simply displayed (with everything in read-only for example).
For now I have 3 templates with 3 controllers for each action (submit / edit / view) and the form is added as a partial (ng-include). The form has also its own controller. Is it the right way?
Also, should I make the form's controller the children of the templates' controllers or the opposite? I am using the same model for each action behind the form and I guess it should be injected through the template's controller.
It's my very first attempt to do this and I would like to have a few advice's and hints since I am afraid of going the wrong way. Thank you!
yo can use different template for each of them with single controller, different controller for each of them is not a good idea because there may be some common function in them then you need to write that function in each of them.
you can use common model for them,i am doing the same.
I see this is promising solutions to what you are looking for
can you try this http://vitalets.github.io/angular-xeditable/#editable-form
I've been trying out zf2 for a couple of weeks and generally enjoy working with it. I have trouble though deciding where to do specific tasks. Does a function belong in the model or in the controller etc.
At the moment I would like to understand the view helper and controller plugin concepts.
Could anyone give a few examples what kind of functionality belongs in a view helper?
I would like to know the same for the controller plugin, a few examples to make me understand why I would make a plugin instead of programming the functionality in the controller?
View helpers extend functionality on the view layer and are for reusability throughout your application.
Controller plugins extend functionality on the controller layer.
You generally use both to keep your controllers / views light and thin and reuse those in your application (keeping your code DRY).
I'm putting together a ZF based CMS at the moment, and am currently caching my Zend_Navigation object, as well as the html rendered by renderMenu(). So at the moment, whenever the menu changes, I have to call the following lines in the relevant action:
$cache = Zend_Registry::get("cache");
$cache->remove("menu");
$frontcache = Zend_Registry::get("frontcache");
$frontcache->remove("menuhtml");
I have a siteController to handle changes to the menu structure, and a pageController to handle add/edit/delete of individual pages, so the code is used in actions in both of these controllers.
I would obviously like to put this code in a single method I can call, but where would be the most appropriate place? An action helper? A parent class for siteController & pageController? Should I combine the controllers? Or something else?
Have you looked at using an Action Helper ( http://framework.zend.com/manual/en/zend.controller.actionhelpers.html )? This will give you a place that's independent of your controllers that each controller will still be able to call.
How about a service? Application_Service_Navigation (or whatever appnamespace you are using) stored in application/services/Navigation.php, implementing an interface representing the CRUD operations in those two CMS controllers. Then internally, these methods can use the cache as you have described. Controllers call the service methods and are unaware of the cache operations.
I have a custom controller action helper that I would like to be able to call from a view script. How can I achieve this?
Solution:
Warning: You probably don't really want to call an action helper from your view script unless you aren't using any dispatch hooks. But if you really, really want to call your action helper:
$helper = Zend_Controller_Action_HelperBroker::getStaticHelper('Myhelper');
Afaik, you can't, and you should not that's MVC. Trying to solve such problem should be a potential warning on your design.
However, in some case you may need to achieve a similar thing.
For example, the flashMessenger() action helper aims to provide a simple way to share messages between requests, however it is not available in the view, you need to manually pass it to the view. I've myself written a wrapper to be able to use as a view helper.
So maybe try to be more explicit on what you're trying to achieve, and we may help you to know if there isn't a good alternative.
i need to show variables in part of layout(i know placeholder is useful!) but this variables assigning in actions of controller, so i need render of view be after action running and this variables will show in all of pages (like menu but not exastly).
some of actions load with ajax!
what is your idea?! what is best way?
If I understand your question, Action Helpers might be what you are looking for. Take a look at the documentation http://framework.zend.com/manual/en/zend.controller.actionhelpers.html