Question

My route setup :

Zend_Controller_Front::getInstance()
    ->getRouter()
    ->addRoute('view', new Zend_Controller_Router_Route('controller/action/:name'))

My link in view :

$this->url(array("name" => "John"), "view", TRUE);

// returns "controller/action/John" as should

Now, when I am at controller/action/John, how do I get the name from URL ? I tried

$this->getRequest()->getParam("name");

but the name param isn't there - getRequest() returns only controller, action and module params.

Was it helpful?

Solution

When you set up your route configuration the route definition should either directly match the controller/action names or be set with defaults. Actually setting the defaults in any case is just good practice and helps you avoid issues like this.

So, in your case according to the comments your route should probably look like this.

$defaults = array(
    'controller'=> 'offers',
    'action'    => 'view',
    'name'      => ''
);
$route = new Zend_Controller_Router_Route('offers/view/:name',$defaults);

As mentioned in the comments you can always check what route has been used with Zend_Controller_Front::getInstance()->getRouter()->getCurrentRouteName(). If it doesn't show your expected route the Router isn't able to find a match and moves on until it usually ends in the "default" route.

As a side note to your question: When you use $this->url(array("name" => "John"), "view", TRUE) you only create the link based on the route. This method is only part of the view and does nothing in terms of dispatching to a controller or action.

OTHER TIPS

For those who found this question and for future reference, you can get params from route using this : $this->params()->fromRoute('param1', 0); in Zend Framework 2 at least. This is what I was looking for in this question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top