3
votes

I'm using Zend Framework, and am trying to set up some custom routes. These seem to be working fine during dispatch, but the reverse matching isn't working at all.

These are the routes I've set up.

$router->addRoute('category', new Zend_Controller_Router_Route(
    'categories/:category',
    array(
        'controller' => 'category',
        'action' => 'modify',
    )
));

$router->addRoute('categories', new Zend_Controller_Router_Route_Static(
    'categories',
    array(
        'controller' => 'category',
        'action' => 'index'
    )
));

If I now go to http://example.com/categories, the correct controller and action are executed (category.index). If I go to http://example.com/categories/5, I get the following error:

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

Adding a default value to :category fixes this problem for reason (suggestions welcome). That is not the major problem though.

Whenever my application executes $this->url(...), the returned value is always the current URL. So, on http://example.com/categories, if I run

echo $this->url(
    array('controller' => 'category', 'action' => 'modify', 'category' => 5)
);

The returned value is http://example.com/categories. I am completely stumped..

The problem persists even if I try to bypass the default router and do

$router->removeDefaultRoutes();
$router->addRoute('default', new Zend_Controller_Router_Route(
    '*', 
    array('controller' => 'index', 'action' => 'index')
));

Does anyone with a bit more experience with Zend routing want to have a crack at this?

Cheers, Jon

2
Can you try to switch the two routes?PeeHaa
Switching the routes did not change much. URLs map to the standard Zend pattern. I can access /categories directly and get the correct page, but /categories/2 yields "category is not specified"... EDIT: Turns out the category is not specified comes whenever calling $this->url without specifying 'category' => ..., even when I'm trying to link to a different controller/action..?Jon Gjengset

2 Answers

4
votes

Try changing this line

echo $this->url(array('controller' => 'category', 'action' => 'modify', 'category' => 5))

into

echo $this->url(array('controller' => 'category', 'action' => 'modify', 'category' => 5), 'category')

the second parameter is route name that url function should use. If its not passed helper will user current route.

@Edit

After deeper research I have found another solution for your problem, if you dont want to pass each time router name your code should looke like this:

$router->addRoute('category', new Zend_Controller_Router_Route(
    'categories/:id',
    array(
        'controller' => 'category',
        'action' => 'modify',
        'id' => 0
    ),
    array(
      'id' => '\d+'
    )
 ));

$router->addRoute('categories', new Zend_Controller_Router_Route_Static(
    'categories',
    array(
        'controller' => 'categories',
        'action' => 'index'
    )
)); 

This will be setting the router part, now call url helper in view.

<?php echo $this->url(array('controller' => 'category', 'action' => 'modify', 'id' => 5), null, true);?><br/>
<?php echo $this->url(array('controller' => 'category', 'action' => 'index'), null, true);?><br/>

The output should be:

/categories/5

/categories

0
votes

For that you have to specify the reverse URL while setting up your router. Like explained here.

I have done this in my project, but using INI file config. Like shown below:

routes.myroute.type = "Zend_Controller_Router_Route_Regex"
routes.myroute.route = "devices/page/(\d+)"
routes.myroute.defaults.controller = "index"
routes.myroute.defaults.action = "devicesindex"
routes.myroute.map.1 = "page"
routes.myroute.reverse="devices/page/%d"

Save this as an INI file in your config folder or somewhere in your APP root.

In your bootstrap, have something like:

public static function setupCustomRoutes() {
    $router = self::$frontController->getRouter();
    $config = new Zend_Config_Ini(dirname(__FILE__) . '/config/routes.ini');
    $router->addConfig($config, 'routes');
}

Use the following code to generate the reverse URL in your view scripts:

$this->url(array('page' => 3));

which will generate

/devices/page/3/

I believe you can modify your code like the following to make the reverse URL to work:

$router->addRoute('category', new Zend_Controller_Router_Route (
  'categories/:category',
  array(
    'controller' => 'category',
    'action' => 'modify',
  ),
  array('category' => 'category'),   //Not sure about this line. Do experiment.
  'categories/%d'
));