views:

883

answers:

2

I'm using Zend FW 1.9.2, want to disable the default routes and supply my own. I really dislike the default /:controller/:action routing.

The idea is to inject routes at init, and when the request cannot be routed to one of the injected routes it should be forwarded to the error controller. (by using the defaultly registere Zend_Controller_Plugin_ErrorHandler)

This all works fine, until I disable the default routes with $router->removeDefaultRoutes(); When I do that, the error controller no longer routes unrouted requests to the error controller. In stead, it routes all unrouted requests to indexAction on the default controller.

Anyone have any idea how to disable the default /:controller/:action routing but KEEP the route error handling?

Basically, this is what I do:

$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->removeDefaultRoutes(); // <-- when commented, errorhandling works as expected

$route = new Zend_Controller_Router_Route_Static(
    '',
    array('controller' => 'content', 'action' => 'home')
);
$router->addRoute('home', $route);
A: 

When you remove the default routes, you remove the default route that the error handler plugin utilizes. This means that when it tries to route to

array('module' => 'default, 'controller' => 'error', 'action' => 'index')

none of your routes match this setup. Thus it'll fail. I suppose you could add just this route from the default like so:

$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->removeDefaultRoutes(); // <-- when commented, errorhandling works as expected
// Re-add the error route 
$router->addRoute(
   'error', 
    new Zend_Controller_Router_Route (
       'error/:action',
       array (
          'controller' => 'error',
          'action' => 'error'
       )
    )
);

$route = new Zend_Controller_Router_Route_Static(
    '',
    array('controller' => 'content', 'action' => 'home')
);
$router->addRoute('home', $route);
PatrikAkerstrand
I checked, but the default errorhandling plugin doesn't inject any routes, so removing the default routes should (in theory) have no effect on it.I tried adding an error route like you did, but nothing changes to the way it works. To be honest, I don't really think your answer makes sense, since this will only match request like /error/asd and /error/qwe etc.Am i missing something?
Maurice
+1  A: 

The problem when you remove the default routes is that Zend no longer understands the urls /:module/:controller/:action, so whenever a route is sent, it gets routed to the default Module, index Controller, index Action.

The Error plugin works on the postDispath method of the controller dispatch and it works because in the standard router if the controller, or module, or action isn't found it throws a error.

To enable custom routes in this config you must write a new plugin that works on the preDispatch, and check if the route and then redirect to the error plugin in the event it's a invalid URL.

Chris
Thanks, this makes sense, I will test it.
Maurice