Wednesday, November 28, 2012

Route default is not defined

Working this long with Zend Framework makes me do things that have become a real routine and I don't pay much attention to it. So when I just add few custom routes to my Zend Framework application, I don't make a big fuss out of it. But apparently people do struggle with it. They get messages like this:

Zend_Controller_Router_Exception: Route default is not defined

Apparently when you create custom routes, Zend Framework "forgets" about the default route, causing this error to appear.

To quickly resolve this issue, you just open up application/Bootstrap.php and add the following to your routines:

    protected function _initDefaultRoutes()
    {   
        $frontController = Zend_Controller_Front::getInstance();
        $frontController->getRouter()->addDefaultRoutes();
    }   

If you already have routine for loading routes, just ensure you add the default routes!

    protected function _initRouteSetup()
    {   
        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();
        $config = new Zend_Config_Ini('/path/to/config.ini', APPLICATION_ENV);
        $router->addConfig($config, 'routes');

        // ensure you load the default routes as well!!!
        $router->addDefaultRoutes();
    }

With this blog article I hope to save a bunch of people a lot of time looking for an example and get on with building awesome applications.



Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 License.