Zend framework 2 toroute pass variable to next controller - zend-framework

After a succes form i want to redirect to another controller, i do that with toRoute. Below there is an example
return $this->redirect()->toRoute('plaatsenrubriek',array('controller'=>'AdvertentieController', 'action'=>'plaatsenrubriek'));
Is there a way to insert a variable in the toRoute to pass to the next controller?

The only way to pass a variable during a redirect would be to add a query parameter to the target route.
$bar = 'bar';
$this->redirect()->toRoute('myRoute', array(), array(
'query' => array(
'foo' => $bar,
)
));
Which would result in /myRoute?foo=bar

Related

How to get the variables in routing in zend framework 1.12

I am new to routing in zf. I don't understand some terms in route
$route = new Zend_Controller_Router_Route(
'author/:username',
array(
'controller' => 'profile',
'action' => 'userinfo'
)
);
$router->addRoute('user', $route);
How do we get :username here? from where do we get this?
Username here is parameter pass by the user. So correct link using this route will be http://somepage.com/author/John. In your controller you can get this variable just like POST and GET variables - $this->getParam('author');
If you want to allow user use link without parameter (default parameter) you can add to array - 'author' => null (i usually use this in pagination)

How to add parameters to controller redirect?

In Zend Framework 2 I want to pass an array of parameters from one action to another within the same controller which I did in ZF1 in the following manner:
$this->_helper->redirector->gotoSimple('foo', null, null, $params);
and in fooAction:
$params = $this->_request->getParams();
In ZF2, trying the various answers I have seen here on SO, I came up with the following:
$this->redirect()->toRoute('home/default', array(
'controller' => 'client',
'action' => 'foo',
'param' => 'bar'),
array('param' => 'bar'));
(trying both the $params and $options arguments of toRoute())
and in fooAction:
$param = $this->getEvent()->getRouteMatch()->getParams();
or
$param = $this->params()->fromRoute());
None works for me. Is there a simple way to achieve what I want (passing parameters with a redirect) or should I go the route of using a container, session or even global variables?
You could use the forward plugin:
http://framework.zend.com/manual/2.0/en/modules/zend.mvc.plugins.html#the-forward-plugin
public function someAction()
{
$returnValue = $this->forward()->dispatch('application/controller/index', array(
'action' => 'other'
));
return $returnValue;
}
public function otherAction()
{
return 99;
}
You will be able to pass parameters too
In the end, what I did was, instead of using route parameters, using query parameters, since the parameters I used where not route related. That solved the problem.

Zend Controller Router : Define variables to point to a different action in one controller

I'm just new with Zend and I have a little trouble with Zend Routers. I've searched about it, but nothing found...
I want to be able to define a router for each defined variable at uri level to point to a different action in one controller.
I'm working with lang and modules so I defined at bootstrap application the next initRoutes function:
protected function _initRoutes()
{
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$defaultRoute = new Zend_Controller_Router_Route(
':lang/:module/:controller/:action',
array(
'lang' => 'es',
'module' => 'default',
'controller' => 'index',
'action' => 'index'
),
array(
'lang' => '^(en|es)$',
'module' => '^(default|admin)$'
)
);
$router->addRoute('defaultRoute', $defaultRoute);
return $router;
}
I want to be able to access forum sections and forum topics by their defined action.
Something like :
mydomain/forum -> forum/index
mydomain/forum/section -> forum/sectionAction
mydomain/forum/section/topic -> forum/topicAction
and also with the lang and module defined at uri level like :
mydomain/lang/module/forum
mydomain/lang/module/forum/section
mydomain/lang/module/forum/section/topic
So I have this :
class ForumController extends Zend_Controller_Action
{
public function indexAction()
{
}
public function sectionAction()
{
}
public function topicAction()
{
}
Then I created the next routes inside the Default_Bootstrap :
$forumRoutes = new Zend_Controller_Router_Route(
':lang/:module/forum',
array(
'lang' => 'es',
'module' => 'default',
'controller' => 'forum',
'action' => 'index'
)
);
$sectionRoutes = new Zend_Controller_Router_Route(
':lang/:module/forum/:section',
array(
'lang' => 'es',
'module' => 'default',
'controller' => 'forum',
'action' => 'section',
'section' => ''
)
);
$topic = new Zend_Controller_Router_Route(
':lang/:module/forum/:section/:topic',
array(
'lang' => 'es',
'module' => 'default',
'controller' => 'forum',
'action' => 'topic',
'section' => '',
'topic' => ''
)
);
$router->addRoute('forumTopics', $topic);
$router->addRoute('forumSections', $section);
$router->addRoute('forum', $forumRoutes);
Now, this only works if I define the lang and module at uri level, but doesn't work if I defined like => mydomain/forum/section | section/topic. This also brings me another problem with my navigation->menu. If I define "forum" as a static variable at router definition, when I hover over at any label defined at navigatoin.xml, the uri level have the same value for every one of them.
I've tried to make a chain like this:
$forumRoutes = new Zend_Controller_Router_Route(
':lang/:module/forum',
array(
'lang' => 'es',
'module' => 'default',
'controller' => 'forum',
'action' => 'index'
)
);
$section = new Zend_Controller_Router_Route(
':section',
array(
'action' => 'section',
'section' => ''
)
)
$topic = new Zend_Controller_Router_Route(
':topic',
array(
'action' => 'topic',
'topic' => ''
)
)
$chainedRoute = new Zend_Controller_Router_Route_Chain();
$chainedRoute->chain($topic)
->chain($section)
->chain($forumRoutes);
$router->addRoute($chainedRoute);
But this doesn't work as I expected.
Any help would be appreciated, thanks.
You are new to Zend. You said it. So here are some explanations:
Ideally the URL on Zend application is:
example.com/controller/action/param-name/:param-value
So in which case, if there is an action called Edit under UsersController, it will be:
example.com/users/edit
if action is add, it will be :
example.com/users/add
when you specify first parameter as a variable, it will collide with controller requests. Example: if you say controller is User but first parameter accepts a value and puts it in emplyees then a request as example.com/employees and example.com/user will both point towards employees controller even if the usercontroller exists! which again is a theory!
What you might want to do is, leave the routes to only accept dynamic values rather routing! You do not want users to route your application but, route user to different sections of the application.
About language then you need to use Zend_Locale which will check for HTML language that is
<html lang="nl"> or <html lang = "en">
Hope things are clear! :)
Here's a quick example which should help you work with routes like that in ZF.
If you are using a default project structure like the one you get when starting a new project using Zend Tool go into the Application folder.
In your Bootstrap.php set up something like this:
protected function _initRouteBind() {
// get the front controller and get the router
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
// add each custom route like this giving them a descriptive name
$router->addRoute(
'addTheDescriptiveRouteNameHere',
new Zend_Controller_Router_Route(
'/:controller/:id/:action/:somevar',
array(
'module' => 'default',
'controller' => ':controller',
'action' => ':action',
'id' => ':id',
'somevar' => ':somevar'
)
)
);
}
My example above is just to illustrate how you would use a route where the controller, the action and a couple of parameters are set in the url.
The controller and action should be resolved without any additional work however to get the 'id' or 'somevar' value you can do this in your controller:
public function yourAction()
{
// How you get the parameters to pass in to a function
$id = $this->getRequest()->getParam('id');
$somevar = $this->getRequest()->getParam('somevar');
// Using the parameters
$dataYouWant = $this->yourAmazingMethod($id);
$somethingElse = $this->yourOtherAmazingMethod($somevar);
// Assign results to the view
$this->view->data = $dataYouWant;
$this->view->something = $somethingElse;
}
So while you will want to make sure your methods handle the parameters being passed in with care (after all it is user supplied info) this is the principle behind making use of the route parameter binding. You can of course do things like '/site' as the route and have it direct to a CMS module or controller, then another for '/site/:id' where 'id' is a page identifier.
Also just to say a nice alternative to:
$id = $this->getRequest()->getParam('id');
$somevar = $this->getRequest()->getParam('somevar');
which wasn't that nice itself anyway since it was assuming a parameter was going to be passed, using shorthand conditional statement in conjunction with action helpers is:
$id = ($this->_hasParam('id')) ? $this->_getParam('id') : null;
$somevar = ($this->_hasParam('somevar')) ? $this->_getParam('somevar') : null;
If you are not familiar with this, the
($this->_hasParam('id'))
is the conditional test which if true assigns the value of whatever is on the left of the ':' and which if false assigns the value of whatever is on the right of the ':'.
So if true the value assigned would be
$this->_getParam('id')
and null if false.
Does that help? :-D

Render view from Zend_form

I try it via my Zend_form:
$output .= $this->_view->render('admin/form.phtml'
, array('id' => $this->getName()
, 'action' => $this->getAction()
, 'method' => $this->getMethod()
, 'enctype' => $this->getEnctype()
, 'data' => array('code' => $code
, 'name' => $name
, 'description' => $description)));
but when i <?php echo $this->enctype; ?> in admin/form.phtml i got nothing.
admin/form.phtml is rendered correctly
Choosing render just displays the output, Zend does not pass your data to the view. But Using view partials, you could achieve that.
From the documentation,
The Partial view helper is used to render a specified template within
its own variable scope. The primary use is for reusable template
fragments with which you do not need to worry about variable name
clashes. Additionally, they allow you to specify partial view scripts
from specific modules.

Zend Framework paginator force $_GET params usage

Is it possible to use paginator with $_GET parameters?
For example i have a route like this:
$router->addRoute('ajax_gallery',
new Routes_Categories(
'/:lang/:category/age/:dep/:cat/:towns',
array(
"page" => 1,
"dep" => 0,
"cat" => 0,
"towns" => 0
),
array(
"dep" => "[0-9]+",
"cat" => "[0-9]+"
)
));
And i'm making request like this via ajax:
http://localhost/en/gallery?dep=9&cat=27&towns=1
But links that returned from results are without ?dep=9&cat=27&towns=1
How to force zend paginator to use passed $_GET params inside pagination link generation?
So that returned links were:
http://localhost/en/gallery/2?dep=9&cat=27&towns=1
http://localhost/en/gallery/3?dep=9&cat=27&towns=1
http://localhost/en/gallery/4?dep=9&cat=27&towns=1
etc...
or even
http://localhost/en/gallery/2/9/27/1
http://localhost/en/gallery/3/9/27/1
http://localhost/en/gallery/4/9/27/1
like they are defined inside route
etc...
Thanks
The view URL helper will always output params as part of the URL (separated by forward slashes) and doesn't, to my knowledge, support the GET parameter format.
I don't know what Routes_Categories class does, but working from the default ZF route classes try this:
$route = new Zend_Controller_Router_Route(
'/:lang/:category/:age/:dep/:cat/:towns/*',
array(
"dep" => 0,
"cat" => 0,
"towns" => 0
),
array(
"dep" => "[0-9]+",
"cat" => "[0-9]+"
)
);
$router->addRoute('ajax_gallery', $route);
The * supports any additional named params after your route. The above assumes lang, category and age are required, and dep, cat and towns are optional. Bear in mind if you want to set cat you have to set dep otherwise the route will get confused which variable is what.
In your controller access the page param via the following, which sets a default of 1.
$page = $this->_getParam('page', 1);
Access the URL via AJAX as: http://localhost/en/gallery/2/9/27/1
If you want the page param, use a named parameter: http://localhost/en/gallery/2/9/27/1/page/2
To get this route to work in your pagination you need to update your paginator view controls to use the right route. See: http://framework.zend.com/manual/en/zend.paginator.usage.html#zend.paginator.usage.rendering.example-controls
Look for the code where the URL is outputted and add the route name to the URL view helper. So replace code like this:
<?php echo $this->url(array('page' => $this->previous)); ?>
With:
<?php echo $this->url(array('page' => $this->previous), 'ajax_gallery'); ?>