Why are you able to redirect to a named route in Laminas, and insert a controller and an action? - zend-framework

In Laminas (Ex-Zend): Why are you able to redirect to a named and specified route (for example 'home') with the and give a controller and an action into the parameters, like this:
$this->redirect()->toRoute('home', ['controller'
=> 'NotHome', 'action' => 'displayAll])
Doesn't this the destroy the purpose of using a named route, if you just overwrite it with your own controller and action?

It won't redirect you directly to this controller, but to URL generated using the given route and params. If route home doesn't have a placeholder for controller param, especially is Literal and not Segment type, the parameter will have no effect on redirection result.

Related

Processing of form with redirect [plesk/zend]

I am using Plesk Sample 1.5-1 as a base, but stuck on how to process POST w/parameters.
My form is a 'text' element and 'ok' submit button and below that is a list that will change based on the value of the 'text' element (external XML call).
Inside IndexController, in the ->getRequest->isPost() area, I have a redirect line:
$this->_helper->json(array('redirect' => pm_Context::getBaseUrl()));
Do I have to manually pass the parameters on this line? Or does the controller know because I created a pm_Form_Simple() and added elements? Right now if I am outside the ->isPost block, the parameters are null, so that is why I am thinking I have to manually pass them along.
Do I need something like this?
$this->_redirector->gotoSimple('my-action',
'my-controller',
null,
array('exampleText' => $form->getValue('exampleText'));
I guess am just not understanding how the POST works.
I have looked at the Zend Guestbook example, but it is different enough from Plesk that I can't mentally translate it...and it doesn't redirect to the same page, it redirects some where else.
Ultimately, I want to set the 'exampleText' parameter with a "start date" and after the POST call, make an external XML call and fill out the list... I can make the XML call, but can't get the workflow around empty form -> fill out form and press "ok" -> post processing
thx!
Turns out that the pm_Form_Simple needs JSON I didn't notice that my orig code had JSON but didn't encode the new code...

add conditional logic to controller in zend framework

i want to add a conditional statement to my layout that tests:
the controller param in the url
the existence of a zend_auth()
what's the best way to achieve that? i have tried testing the $this->_getParam('controller') in the layout but got an error. i could just set that variable in all the controllers but that seems kind of dumb. how to best set a variable that i could use later from the layout with some conditional logic? or should i instead add my conditional logic that is inside a view helper and then loaded into the layout?
Edit The controller shouldn't be a URL parameter, unless you are doing some very strange routing. If you were getting a GET (or POST) variable, you would use ->getParam() on the request object, Zend_Controller_Front::getInstance()->getRequest(), as used below. But the controller is a separate property of that request object.
This is the auth part:
$loggedIn = Zend_Auth::getInstance()->hasIdentity();
This is the controller part:
$controller = Zend_Controller_Front::getInstance()->getRequest()->controller;

Zend framework multiple routes for same controller

I'm trying to set up Zend pagination on my site so that I can use Paul Irish's jquery infinite scroll plugin, but I'm having trouble with my routes. I currently have these routes set up for my organizer page:
//Organizer searches
$route = new Zend_Controller_Router_Route('organizer/index/:filter/:page',
array('controller'=> 'organizer',
'action'=> 'index'));
$router->addRoute('organizer', $route);
$route = new Zend_Controller_Router_Route('organizer/index/:filter',
array('controller'=> 'organizer',
'action'=> 'index'));
$router->addRoute('organizer', $route);
It matches organizer/index/popular correctly in this order, but if I put a page number on it the filter suddenly comes up null. If I switch the order, organizer/index/popular/2 works perfectly fine but organizer/index/popular no longer works. I could just only use the more specific route since that's the one I'll need for pagination, but I would like to include both to accommodate users trying to type the url or in case I forget to change the links somewhere in my code. Can I incorporate multiple routes to the same controller with Zend? If so, what am I doing wrong?
You need to give the routes different names. You've called them both 'organizer', so the second one replaces the first each time.
You could also easily do this with one route simply by setting a default value for the page variable:
$route = new Zend_Controller_Router_Route(
'organizer/index/:filter/:page',
array(
'controller'=> 'organizer',
'action'=> 'index',
'page' => 1
)
);
$router->addRoute('organizer', $route);
Every route you add to the router must a have a unique name, so the second route you want to add must have a different name, because with your current code you overwrite the route organizer. Change the second call of $router->addRoute() to something like this:
$router->addRoute('organizer2', $route );

Zend Framework + Tricky controller name and module name conflict

We have a Zend application that has these following modules:
Users
Shop
etc...
Front - A content management module
While the Front module has the following controllers:
UsersController
ShopController
AuthController
etc...
In the middle of our development cycle we decided to set the default module for the Zend application to the Front module, but inadvertently broke our links, as http://domain.com/front/users/list are now generated as http://domain.com/users/list, which is now pointing to the wrong action.
We are generating links using the URL view helper, (i.e. $this->url(array('module' => 'front', 'controller' => 'users', 'action' => 'list'));), but the 'front' URI segment is omitted since switching the default module to the Front module.
I totally understand why this is so, but we are avoiding renaming all controllers under the Front module to avoid conflicts.
My question is, is there is a way to instruct the URL view helper to always include the 'front' module URI segment even if it is already set as the default one?
My question is, is there is a way to instruct the URL view helper to
always include the 'front' module URI segment even if it is already
set as the default one?
You can create your own url view helper with same name and override default url view helper add it to Zend_View object in your bootstrap.
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view)
{
$viewRenderer->initView();
}
$view = $viewRenderer->view;
$view->addHelperPath('/path/to/some/helpers', 'My_View_Helper');
Now create class My_View_Helper_Url let it extend Zend_View_Helper_Url override url method.
Here is reference form ZF doc about this procedure
In fact, you can "stack" paths using the addHelperPath() method. As
you add paths to the stack, Zend_View will look at the
most-recently-added path for the requested helper class. This allows
you to add to (or even override) the initial distribution of helpers
with your own custom helpers.
Having said that I think http://domain.com/users/list, should have worked correctly in first place since you have specified default module to front.

Grails - Render a template by email

I have a controller's method which renders a template.
This works fine to render the template within my .gsp view.
I am also using the mail-plugin, and I would like to used the same controller's function to render the template by email, hence populating some email with it.
I know how to do that from a .gsp view via Ajax request but do not know any way to do that from within a controller or a service.
The idea would be to use my controller's action more like a function, take the rendered teplate and populate my email with it.
Also, my controller's action needs to have some 'params' properties to work properly.
Any suggestion most welcome.
Regards,
You can use the render tag( http://grails.org/doc/latest/ref/Tags/render.html ) can be used to return a string.
I would move whatever logic you have in your controller that is reusable into a service, and then use this to return a model, then you can simply call this via:
def model = myService.method( ... )
def emailContent = g.render( template: 'mytemplate', model: model)