Hello, everyone.
I'm on ZF 1.9.5 (Debian unstable), and I'm trying to disable the default
controller/action routing.
My goal is to only return 404 pages for everything except for the routes
I have explicitly defined myself, for example in an .ini file.
From the documentation, one might belive that calling
removeDefaultRoutes() on the front controller router might do it.
http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.default-routes:
"If you do not want this particular default route in your routing
schema, you may override it by creating your own 'default' route (i.e.,
storing it under the name of 'default') or removing it altogether by
using removeDefaultRoutes():
// Remove any default routes
$router->removeDefaultRoutes();"
However, that leaves you with a situation where the ErrorHandler plugin
does not seem to kick in, as now every URL gives you the "index" action
of the "index" controller, and you get no 404 at all.
I would expect every URL to to give a 404 at this point.
http://framework.zend.com/issues/browse/ZF-3465
However, looking at bug #3465, one gets the impression that my
expectations may be wrong, that what happens is the correct behavior.
Though it seems other people beside myself also feel confused about this
behavior.
The bug contains a patch right for Zend_Controller_Dispatcher_Standard
to get the expected behavior, but I don't want to fork the library itself.
Looking at the code and the patch, it seems like the current behavior
comes from the Dispatcher.
I'm not quite sure how the Dispatcher and Router fits together, but I
was able to get the expected behavior by overriding the dispatcher.
My attempt at getting the expected behavior left my Bootstrap.php:
class CustomDispatcher extends Zend_Controller_Dispatcher_Standard
{
public function dispatch(Zend_Controller_Request_Abstract $request,
Zend_Controller_Response_Abstract $response)
{
$controller = $request->getControllerName();
if (empty($controller)) {
throw new Zend_Controller_Dispatcher_Exception('URL not
found in routes');
}
return parent::dispatch($request, $response);
}
}
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initDispatcher()
{
$dispatcher = new CustomDispatcher();
$front = Zend_Controller_Front::getInstance();
$front->setDispatcher($dispatcher);
return $dispatcher;
}
public function _initRouter()
{
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$router->removeDefaultRoutes();
$router->addRoute('user', new
Zend_Controller_Router_Route('author/:username',
array('controller' => 'brille', 'action' => 'index')));
$router->addRoute('index', new
Zend_Controller_Router_Route('/', array('controller' => 'index',
'action' => 'index')));
return $router;
}
}
This seems to work, but I have yet to test it properly.
I guess my question is; is this a viable way of disabling the default
routing scheme in order to get "only-defined-routes" routing ?
Thanks in advance
--
Torbjørn