views:

3182

answers:

1

At the moment in my ZF project have a URL structure like this:

/news/index/news_page/1/blog_page/2

When I generate my pagination I use the URL helper as follows:

<?php echo $this->url(array('blog_page'=>3)); ?>

Which generates a URL like this:

/news/index/news_page/1/blog_page/3

What I'd like to do is use a custom route to have nicer URLs, something like this:

new Zend_Controller_Router_Route(
  'news/:news_page/:blog_page', 
  array('controller' => 'news', 'action' => 'index')
);

However, when I try an use this route in the view helper:

<?php echo $this->url(array('blog_page'=>3), 'newsIndex'); ?>

It throws an error because I've not specified news_page in the params.

How can I get around this, and tell the url helper to use the 'current' values for these params?

+5  A: 

The url helper will use the an existing parameter if it exists in the current request. It seems as if, in your particular case, the news_page param is not set in the request object. Setting a default value for the news_page parameter in your route should solve your problem.

So, your route definition should look something like this:

new Zend_Controller_Router_Route(
  'news/:news_page/:blog_page', 
  array('controller' => 'news', 'action' => 'index', 'news_page' => 1)
);
jason
Thanks Jason, that's exactly what was happening.
Ciaran McNulty