0
votes

I have 2 similar routes set up for my ZF application, one of which works and one of which doesn't. This one, which displays an item from the database for the user to view, works perfectly:

Route in my bootstrap:

//Static item profile
    $route = new Zend_Controller_Router_Route('item/:item_id',
                                                array('controller'=> 'item',
                                                'action'=> 'index'));
    $router->addRoute('item', $route);

And where I get the item id in the item controller's index action:

$id = $this->getRequest()->getParam('item_id');

I have this one set up in my back end to allow admins to edit item data:

//Management back end
    $route = new Zend_Controller_Router_Route('manage/edititem/:item_id',
                                                array('controller'=> 'manage',
                                                'action'=> 'edititem'));
    $router->addRoute('manage', $route);

And this is where I get the id in the manage controller's edititem action:

$item_id = $this->getRequest()->getParam('item_id');

For some reason the item_id in this one always comes up null if I navigate to /manage/edititem/:item_id, though it works perfectly fine if I go to /manage/edititem/item_id/:item_id. Why would the route not work if I try to use the custom route? It's not getting caught by an earlier route because it's still executing the code inside the edititem action, I'm not missing any semicolons above it, and I copied and pasted it over, so I doubt it is a typo. What could be wrong with my route?

2
Check with $this->getFrontController()->getRouter()->getCurrentRouteName() in your controller, which of your routes are matched. Also check that your routes added in the right order (most generic at first).ByteNudger
For some reason it always comes up as default. It's not matching another route, but it doesn't seem to be catching this one either for some reason.jaimerump
Looks like your route isn't added. You can verify if all routes you defined/add are available with $this->getFrontController()->getRouter()->getRoutes() (you should use array_keys() to only print the route names). If the route isn't there you should check your code in the Bootstrap.php, because somehow you maybe overwrite a variable or didn't add the route. You should print the names in your Bootstrap.php and controller to see if something changing your routes between these two.ByteNudger

2 Answers

0
votes

Jaime,

Your code looks right... I've just tested it on my local instance, and the reroute worked properly for

$route = new Zend_Controller_Router_Route('manage/edititem/:item_id',array('controller'=> 'manage','action'=> 'edititem'));

This leads me to think the problem lies elsewhere. The first thing I would try is to disable all other routes in your bootstrap, and see if there is really no conflict there.

Another something to try is to use a different field name than item_id. Maybe somehow the router manager is getting confused if there is more than 1 route with the same field name that it needs to pass to the controller.

0
votes

Since posting this question I have rewritten several of my routes, but I believe the problem was the name I set here.

$router->addRoute('manage', $route);

I wasn't aware of the restriction at the time, but with the zend router each name has to be unique, and I believe I had 2 routes named manage at the time. Most of my routes are still the same, but they have more descriptive names now and I'm no longer having this problem.