How to pass select from one controller to another in Zend? - zend-framework

currently I'm using an output controller for simple HTML outputs. There is another report controller for output as PDF. Both controllers use the same SQL and are filtered by session data.
Is there a way to pass the Zend_Select object from the list controller to the report controller? So I don't have to assemble the select in each controller again.
As I remember it's not possible with the registry. But maybe there exists another elegant way?

you can use smth like a abstract base controller for the list and report controller, that would be helpful.

Related

How to call a Mojolicious controller method after failing on template

I have a Mojolicious problem that I suspect has an easy solution, but I can't swim through all of its code.
$r->get(
'/:controller/:action',
sub {
my $c = shift;
$c->render_maybe && return;
# No template. Either call controller->action() or dispatch as POST
}
);
$r->post('/:controller/:action');
As you can see, I have two routes that use the same URL, one for GET and one for POST. The POST is straightforward. It renders after finding the controller and action method and isn't concerned with a template. I have the GET method working where a template exists, ignoring the controller, by using a callback with render_maybe(). My issue is, if there isn't a template, I want to go ahead and run the controller's method.
One solution would be to simply identify my controller and call the action. Since I'm using placeholders in my route, I can't simply hard code this. So, is there a Mojolicious way of getting my controller class, or actual code that will get the class and call the method? I have both controller and action defined in stash, so this really shouldn't be a big deal. Mojo knows how to do this internally.
Another option would be to convert this to a POST method and run it as normal. I don't know if it's best to either come up with the URL, or find the defined POST route, or just convert my GET to a POST. I'm not sure how to accomplish any of these.
Thanks.
I'm not sure if this is the intended use case for under(), but it seems to be a decent solution to my problem.
$r->under( '/:controller/:action', sub { !shift->render_maybe } )->get('/');
$r->post('/:controller/:action');
Chaining under()->get() will create a nested route. The first in the stack will render a template, if it exists, from the render_maybe, stopping there. If the template doesn't exist, it will go to the standard get(), which will first check for a controller action. This is exactly what I was wanting.

Swift - WatchKit: How to get data back to the root view controller?

Suppose I have three interface controllers which are all linked by push segues.
InterfaceController1 -pushSegue-> InterfaceController2 -pushSegue-> InterfaceController3
InterfaceController1 has table which will house the data from the class below.
So, suppose I have class:
class manageData {
var data1:String?
var data2:String?
}
and this class is supposed to hold the data from InterfaceController2 and InterfaceController3.
So InterfaceController1 creates a manageData object and passes it to InterfaceController2 using the contextForSegueWIthIdentifier method. InterfaceController2 then adds to its data to the manageData object and passes it along to InterfaceController3 using the contextForSegueWIthIdentifier method as well.
Now my question is, once InterfaceController3 adds its data to the object, how do I pass that complete object back to InterfaceController1 with the data filled in so InterfaceController1 can add the data to the table? Hopefully I've made this clear enough. I don't have any actual code to represent this because I have no idea how to implement it yet.
So the way I solved this issue was to create a singleton class which held all my data throughout the different controllers. Then when I reach the last controller and send my data to the singleton, I pop back to the root controller.
When I'm back in the root controller, in the awake method, I call a function I made called loadTableData() which automatically takes information from the singleton when it awakes and loads it in the table. Seems to work perfectly for now but I don't know any potential issues that can come up with this method.

How to create instance to zend controller

I have a controller named class TestController which extends some Zend_Controller_Action. Now I would like to use create an instance of TestController in TestForms (a Zend_Form). I want to populate a Zend_Form_Element_Select dynamically.
Please suggest how I can do this. Thanx in advance.
Where are you instantiating the form - is it in the controller? Instead of having the form call an action on the controller to dynamically get the values, you should look at setting the values on the form after it has been instantiated.
A quick and dirty way of doing that would be to grab the values in the controller and assign it to the element via:
$values = $db->query('query');
$element = $form->getElement('dynamicSelect');
$element->setValue($values);
Of course having DB queries to a table in your controller isn't exactly best practice... Per philistyne's suggestion, I use a a form builder class to build forms dynamically from my models. I have mappers for each model, and I pass in the mapper to the form builder class so it can dynamically populate my select elements.
A couple of things to try (passing a controller into a form or instantiating from within one is not recommended):
Use a model to access the dynamic values you want to put into your Zend_Form_Element_Select.
If the form is complex, create a form builder class to take care of, and separate out, the heavy lifting of the form construction.
Create customised form elements by extending from Zend_Form_Element_(Radio, Select, etc etc) if you feel you need very fine control over the form element's construction/behaviour/appearance, but wish to be able to reuse that element elsewhere.

Is it possible to route my simple URL (without specifing the module, controller and action) in Zend framework?

I am using zend framework for my project and I have a requirement to route the path where I want it. For example:
I have a path www.example.com/module/controller/action1 and I want to internally route in to www.example.com/module/controller/action2.
But the main problem is I do not want to use the function in which I have to specify the module, controller and action [$this->_forward('action2', 'controller', 'module');], simply I something like this: $this->_forward('module/controller/action2');.
Please suggest me if anybody has its solution. its urgent need of my project.
Thanks,
Jitu
The controller method _forward() can accept only the action if it is in the same controller and module as the first action you are forwarding from.
So in your action1Action() method, you can simply call:
$this->_forward('action2');
If your second action is not in the same module/controller, you could subclass Zend_Controller_Action into your own "base" application controller that all your other action controllers inherit from (a good practice on ZF projects I find) and then create another one called _forwardFromUrl() or something like that, which breaks your URL apart and passes it to _forward() (or create an action controller helper if you just need this one extra thing).
This example is simplified and assumes your $url will always be in the format module/controller/action:
protected function _forwardFromUrl($url)
{
$parts = array_reverse(explode("/",$url));
$this->_forward($parts[0],$parts[1],$parts[2]);
}
Hope that helps!

Pass "DirectoryInfo" object into Controller from a View?

I'm new to MVC2 and was wondering if it was possible to pass an object into a Controller Action from a View?
e.g.
I've created a ViewModel containing a list of DirectoryInfo objects and pass this to my Index view.
In the view, I loop through all of the DI objects and for each one create an ActionLink.
<%:Html.ActionLink(linkText: subfolder.Name,actionName: "Reports",
routeValues: new {folder=subfolder}, htmlAttributes:null )%>
but in my Reports action, the "folder" is always null?
public ActionResult Reports(DirectoryInfo folder)
{
//folder is always null here
Is this type of thing possible, or does the routingValue always have to be a primitive?
(ps. I have searched StackOverflow and t'internet and although I can find people asking the same question, I can't find a solution
DirectoryInfo is a complex object and I don't think you will be able to put it directly into HTML. You need to send something simple like path of directory or file from which controller can construct the required DirectoryInfo again.