When I use zend framework, how to run action code in controller before rendering view?
Now I use 2 action functions,
The first one have no view. The second one have the expected view.
When the first function is called, at the end of the function it will be redirected to call the second function.
Although I find this method works, the user cannot go back to last page by browser back button.
i don't understand your question so much but ...
Controller is always run before render action. Its a logic which running before render is called. I dont know from your question what you really need =( try be more objective pls. If you need run any code before action in controller on all actions in controller, you use init or preDispatch functions.
http://zfreak.wordpress.com/2011/04/14/usage-of-init-vs-predispatch-methods-in-zend-front-controller-zend-framework/
If you need 2 separated logics, you can make own function or class in your project and call it or create instance anywhere you need. If your problem is with any render as ACL restrictions... i use own function in controller which retrieve error_access_page and after call $this->_helper->viewRenderer->setNoRender(). If my users not have access, this show error_page and no render action's phtml file.
hope help =]
Related
I have a Home Page, inside that I have created one component RecentlyViewedProductComponent.
My problem is:
when I navigate to /home by using
this.router.navigate(['/home']);
the ngOnInit() inside RecentlyViewedProductComponent is not working. When I close the app and open it again, that only it is working .
How to solve this problem?
It sounds like you are not using the right lifecycle event.
Have you looked at the documentation here:
https://ionicframework.com/docs/angular/lifecycle
It says that ngOnInit() is:
Fired once during component initialization. This event can be used to initialize local members and make calls into services that only need to be done once.
If you want it to be called every time you navigate to the home page then you want to replace this with something like ionViewWillEnter():
Fired when the component routing to is about to animate into view.
There is actually some guidance at the end of the docs page that you might find interesting which explains when to use each life cycle method.
I would like to create a jumpbox block with some form and put it within the layout sidebar. The form will be have an entity select and exactly the Go button.
Base on the documentation I need to render the form template by using {{ render(controller(...)) }}, but I really don't where tu put the form logic.
It is good to create a method in the controller that use entities from the select? but it looks I need to create two methods, the first one for form rendering (without any route) and the second one for the form submit request (with route for "POST" method)?
Can somebody provide me some tips how to do it right way?
First you will need a controller as you sad, this will render this part of the sidebar, and the action should be the same controller, so just simply create a router for it.
And you were also sad right, create a hidden field and set there the current route. But this is the tricky part, cause when you call this "sub" render, inside the controller the route will be always, what is the route for the controller, so what you need to do, when you render the controller you need to pass in a variable what is the current route, what you can do easily by passing the {{ app.request.attributes.get('_route') }} variable value, what is the NAME of the route, and then in your controller, at the end you return a new RedirectResponse($this->generateUrl($url)).
And both, the render of the form and the "process" can be in the same controller, or if you prefer it, you can take it apart, but I would use only one, and you can test from the request, what is the current method, if POST then you will search for the variable and set the session/cookie/ what do you have.
EDIT:
Even though you didn't like, you have to say it's a good answer, but here is an other one.
The action should be always the current route, and basically you need to set up a request event listener. Check there if it's a post method and if yes, then look for your specific key, and there you go. Both is equally good and I used both of them.
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.
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.
I have a controller that passes input from a form into a model class to perform validation.
If the validation is successful I want to allow the flow to continue and render the default view associated with the controller.
My issue is that if validation is not successful then I want the model to pass back validation error messages and display them in a separate view. How can I set the error messages on the alternative view?
Thanks in advance.
Well, from the controller you can redirect them to another action in another controller:
$this->_forward($newactionname,
$newcontrollername,
$newmodulename,
Array($parameters_to_pass);
}
or you just just render a different view file:
$this->render('index_alternative');
Don't use _forward() if you are redirecting to actions in the same controller, just call the action directly using $this->fooAction(), rather than this->_forward('foo'...
The reason is performance and errors that can occur due to the controller being constructed wtice. When you call _forward not only the predispatch run again (which is something to be expected) but init() and the constructor also gets called again. If you have your controller extend from other controllers, than all those controllers will be called too, including their init(). If you have code in your init() is it will run twice, and if you are writing to a database it will write the line twice! Avoid the whole thing and call the action directly and use $this->render() instead.
You can easily see this issue if you profile your code,
Why do you want to display the error messages in a different view? Why not build conditionals into the view? Something like if form has errors then echo messages else echo form.
You could use $this->_forward to forward to another action with its respective view. You can pass along whatever you wish. Just pass the form object along, it contains all the error messages. Or you can retrieve certain error messages or all of them from the form object and pass them to a view or an action.
FlashMessenger could be part of the solution?
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html