1

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.

9
  • Have you tried $this->getRequest()->getPost() $this->getRequest()->getGet()? Commented Oct 25, 2012 at 14:44
  • It is empty - getRequest() checks GET and POST too. Commented Oct 25, 2012 at 14:47
  • 1
    Probably some other route kicked in. Chechk with Zend_Controller_Front::getInstance()->getRouter()->getCurrentRouteName() if the view route is actually used Commented Oct 25, 2012 at 14:50
  • Yes, the route is set to "default" indeed. Hmm, why doesn't it change to "view", when I use "$this->url(array("name" => "John"), "view", TRUE);" ? Commented Oct 25, 2012 at 14:54
  • What you add in the view script doesn't matter. It is the router who checks the URL against the request. If it picks the "default" route it doesn't find a match for your view route. The controller/action URL must match an existing controller name and action name. What are the real names and do they match? Commented Oct 25, 2012 at 15:53

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.