Zend framework: removing default routes

I am using Zend FW 1.9.2, I want to disable the default routes and provide my own. I really don't like the default route /: controller /: action.

The idea is to enter routes in init, and when the request cannot be redirected to one of the entered routes, it should be redirected to the error controller. (using the default case of Zend_Controller_Plugin_ErrorHandler)

Everything works fine until I turn off the default routes using $ router-> removeDefaultRoutes (); When I do this, the error controller no longer sends unsolicited requests to the error controller. Instead, it redirects all unsolicited indexAction requests to the default controller.

Can anyone think of how to disable the default routing /: controller /: action, but DON'T GET route error handling?

Basically, this is what I am doing:

$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); 
+4
source share
3 answers

The problem with deleting default routes is that Zend no longer understands the urls /: module /: controller /: action, so whenever a route is sent, it is redirected to the default module, index controller, Action index.

The Error plugin works on the dispatch manager's postDispath method, and it works because in a standard router, if a controller, module or action is not found, it throws an error.

To enable custom routes in this config, you must write a new plugin that works in preDispatch and check if the route was then redirected to the error plugin in case it is an invalid URL.

+5
source

When deleting default routes, you delete the default route that uses the error handler plugin. This means that when he tries to switch to

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

none of your routes match this setting. Therefore, it will fail. I suppose you could only add this route from the default:

 $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); 
0
source

I ran into the same problem for an old application, here is what solved my problem:

 $front = Zend_Controller_Front::getInstance(); $router = $front->getRouter(); $route = new Zend_Controller_Router_Route('*', array('controller'=>'error', 'module'=>'error', 'action'=>'notfound')); $router->addRoute('default', $route); // After that add your routes. 

You need to add this route first, as it must be processed last.

And in ErrorController I defined:

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

Thus, any route that does not match our routes will use the default error handler.

-one
source

All Articles