Question

I have some trouble with the router.

I have a custom route :

$router->addRoute('showTopic',
                      new Zend_Controller_Router_Route('/forum/topic/:topic',
                                        array('module' => 'forum',
                                              'controller' => 'topic',
                                              'action' => 'show'),
                                        array('topic' => '\d+'))); 

But when I try to access this url : localhost/forum/topic/16 I get this error :

Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with message 'topic is not specified'

But I don't want to put a default value for topic, because I also want the route /forum/topic to list all topics...

Secondly, I know that if I add a custom route, the default router is overridden, but I need to have some default routes too. The only way I have found is to set 'default' in the second parameter of the url view helper, like this

$this->url(array(
                 'module' => 'forum',
                 'controller' => 'topic',
                 'action' => 'add'
                ), 'default', true)

Is there a more elegant way instead of doing this for all url where I want to use the default behavior ?

Was it helpful?

Solution

You should have a default value for a topic and add the more general route (the one for forum/topic) after the more specific one. Route_Rewrite checks the routes beginning with the last one (it actually does an array_inverse).

The url helper delegates assembly urls to a route, its second paremeter being the name of the route to pull from the router. Since the default route is registered under the name of 'default', there is nothing really inelegant in using the name (it is not a magic string or a special case). If this really bugs you, you could write a custom helper (to be placed under "views/helpers"):

class Zend_View_Helper_DefaultUrl extends Zend_View_Helper_Abstract {
   public function defaultUrl($params) {
      return $this->view->url($params, 'default');
   }
}

And use it in your view like defaultUrl(array('action'=>'test')) ?>.

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