Using a Separate Routes Configuration File

Published on July 22, 2012 by

In the Zend Framework, there are many ways to add custom routes to a project. One way is to simply add them to application.ini. However, as projects grow in size, so does the configuration file along with the number of custom routes. Therefore it will often be useful to specify custom routes in a separate configuration file. In this article we are going to look at two ways of accomplishing this. The first method involves a small addition to application.ini, while the second adds logic to the bootstrap file.

Changing application.ini

This is definitely the easiest and simplest way to add a new configuration file for custom routes. It involves just a single addition in the configs/application.ini file. Simply add the following to the [production] section:

config.routes = APPLICATION_PATH "/configs/routes.ini"

This defines the path to our new configuration file. Next, create a new file named routes.ini in the configs folder. In this new file you will specify your custom routes, like shown below.

[production]

resources.router.routes.register.route = /register
resources.router.routes.register.defaults.module = default
resources.router.routes.register.defaults.controller = user
resources.router.routes.register.defaults.action = register

[staging : production]

[testing : production]

[development : production]

Be sure to include all of the sections. If you do not, for instance, include the development section and your application environment is set to development, then an exception will be thrown. Also, be sure to prepend your routes with resources.router. with this method, just like they would in application.ini. That is it! Your application should now be running with a separate route configuration file.

Changing the Bootstrap

If you created your project with the Zend Framework CLI Tool, you are ready to go and may skip the next two steps. If not, then make sure that you have a Bootstrap.php file in your application folder (or whatever your APPLICATION_PATH is set to). Next, make sure that you have the following entries in the [production] section in your config/application.ini file:

bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"

Now we are ready to create our new configuration file. Create a new file called routes.ini in the configs folder and open it. Here we will add our routes like shown below.

[production]

routes.register.route = /register
routes.register.defaults.module = default
routes.register.defaults.controller = user
routes.register.defaults.action = register

[staging : production]

[testing : production]

[development : production]

This looks much like we did with the first method, but note that the preceding resources.router. is missing. Let us now focus our attention on the bootstrap file. Add a _initRoutes() method like in the code block below.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
	// Load routes from configuration file
	protected function _initRoutes() {
		$front = Zend_Controller_Front::getInstance();
		$router = $front->getRouter();
   
		// Load our new configuration file
		$config = new Zend_Config_Ini(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'configs' . DIRECTORY_SEPARATOR . 'routes.ini', 'production');

		// Add our custom routes to the router
		$router->addConfig($config, 'routes');
	}
}

The parameters to the Zend_Config_Ini constructor are the configuration file path and the section to load, respectively. If the section parameter is left out (or set to null), then all sections will be loaded.

That is all there is to it! Thank you for reading.

Featured

Learn Zend Framework today!

Take an online course and become a ZF2 ninja!

Here is what you will learn:

  • Understand the theory of Zend Framework in details
  • How to implement an enterprise-ready architecture
  • Develop professional applications in Zend Framework
  • Proficiently work with databases in Zend Framework
  • ... and much more!
Zend Framework logo
Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

Leave a Reply

Your e-mail address will not be published.