Setting up for a Modular Directory Structure

Published on September 1, 2012 by

Using a modular directory structure allows code to be separated into self-contained units. It can be thought of as independent smaller MVC applications within your main application. For instance, a website may have a blog and admin module besides the default module. When creating a new project by using the Zend Framework CLI Tool, a modular structure is not present by default. In this article we will show how to change this by changing the project’s configuration file.

Editing the Configuration File

The first thing to do is to change the application/configs/application.ini file so that the framework knows to use a modular structure and where to find the modules. In the [production] section, add the two lines below.

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = 

Creating the Required Directories

Now we need to create the required directories. First, we need to create the directory which will contain our modules; modules as per our configuration file. Create the following two directories:


Now we have created the directory for the default module, but we need to add folders within it to contain models, views and controllers. If you used the Zend Framework CLI Tool to create your project, you can simply cut and paste the models, views and controllers folders that were automatically created within the application folder. Otherwise you will have to create the folders manually like discussed in the section below.

Creating the Directories By Hand

If you are creating your project by hand, then create the below folders.


If you manually created the above folders, then you need to create a controller and a view script. Create the file application/modules/default/controllers/IndexController.php and fill it with the code below.

class IndexController extends Zend_Controller_Action {
	public function indexAction() {

The indexAction will automatically look for the view script application/modules/default/views/scripts/index/index.phtml, so create that file with your desired HTML content. Also note that for exceptions (e.g. incorrect URLs), you should consider creating an ErrorController along with a view script to handle these gracefully.

Testing the Modular Application

Now that everything is ready, it is time to see the modular application in action. Try to visit your website’s front page. Entering localhost (or wherever your site is located) into your browser’s address bar should show you the front page. Behind the scenes, the router is automatically routing the request to your-domain/default/index/index. Enter this in your address bar and you should see the same page.

Congratulations, you are now ready to develop modular applications in the Zend Framework!


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.