Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework: How to disable default routing?

I've spent many hours trying to get this to work. And I'm getting quite desperate. Would be great if someone out there could help me out :)

Currently using Zend Framework 1.9.5, though I have been struggling to get this to work for many versions now.

What I want to do is provide my own routes through an XML config, and make sure that everything that is not defined in my config will end up on my errorController. (preferably in a way so I can em apart from EXCEPTION_NO_CONTROLLER and EXCEPTION_NO_ACTION)

I figured that this means I have to get rid of default /:module/:controller/:action and /:controller/:action routes.

So when I tell the router to removeDefaultRoutes(), it won't match these default routes anymore. But now the router is now routing every unrouted route to the defaultcontroller::defaultaction (What the ??)

$front->getRouter()->removeDefaultRoutes();

So, anyone know how to make the frontcontroller (or a part of it) throw an exception when an URI can not be routed?

Reason I want to do this is to prevent duplicate content, and have better 404 pages (in this case, no controller / no action errors are actually application errors instead of not-found)

like image 232
Maurice Avatar asked Nov 25 '09 14:11

Maurice


2 Answers

did you try adding a new route like

$route = new Zend_Controller_Router_Route('*', array('controller'=>'error', 'module'=>'default', 'action'=>'error'));


$router->addRoute('default', $route);

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

like image 65
prodigitalson Avatar answered Oct 13 '22 14:10

prodigitalson


Fast forward in time to one year later... (time travel music)

Here's another way that I think is much less "intrusive". You can write a plugin to catch the default route and when that happens just throw an exception which at the end of the whole cycle gets translated into a 404 by the front controller.

class Application_Plugin_DisableDefaultRoutes extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $front = Zend_Controller_Front::getInstance();
        $currentRoute = $front->getRouter()->getCurrentRouteName();
        if ($currentRoute == 'default') {
            throw new Exception('Default route is disabled');
        }
    }
}

You can load your plugin in Bootstrap.php

protected function _initPlugins()
{
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new Application_Plugin_DisableDefaultRoutes());
}

With this way you can load the plugin in the production machine and leave it out in development where you might want to use the default route for quick tests or something else.

like image 23
Julian Avatar answered Oct 13 '22 13:10

Julian