Php – Zend framework: Removing default routes

front-controllerPHPurl-routingzend-framework

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);

Best Answer

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.

Related Topic