4
votes

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

3 Answers

4
votes

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.

0
votes

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

I encountered the same issue for an old application, here is what solved my problem:

$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$router->removeDefaultRoutes();
// forward all routes to the not found error action
$route = new Zend_Controller_Router_Route('*', array('controller'=>'error', 'module'=>'error', 'action'=>'notfound'));
$router->addRoute('default', $route);
// After that add your routes.
$route = new Zend_Controller_Router_Route_Static('', array('controller' => 'content', 'action' => 'home'));
$router->addRoute('home', $route);

You need to add this route first as it needs to be the last processed.

And in ErrorController I defined:

public function notfoundAction()
{
    throw new Zend_Controller_Action_Exception('This page does not exist', 404);
}

This way any route that is not matched to our routes will use the default error handler.