Zend Framework 1 tutorial Part 2: The directories structure

Zend Framework 1 Logo

My goal was to create a structure that is very easy to understand, flexible enough for big or small websites and not totally different from what books or other guides recommend as a "files and folders structure for a Zend Framework project" best practice.

Another goal was to create a CMS core, which has lots of modules that can be (re)-used on (multiple) other websites. Every module is designed so that it can be copied and put in another project without having to modify any file of that project. The main directory of each module contains all the files it needs to work, I has its own bootstrap, its own configuration files, its views and of course its own models.

This post contains the recommended structure for an application developed using the Zend Framework based on the (ZF) quick start, but there are several changes made by me. I won't use the application/controllers folder, I will put my default controllers in the homepage module directory which is my default module. As you can see the modules have a MVC.


Models can be used to read and write data into a MySQL database, but they can also be used to read / write data in other types of storages like a NoSQL database, an XML file or even an external API (a service). The models directory has several sub folders, for mongoDB models, for MySQL (or MariaDB) models, for session management models and another one for the xml (read / write) models. In this project we will only need and create MySQL models.


The views folders in each module directory contain the view scripts for the actions and there is a helpers folders for module specific helpers. The helpers that can be used website wide are put in the helpers folder that is in the layout directory.


The controllers directory will contain the controllers. The default Module (which is the homepage module) will only have an IndexController. All the other modules will have at least two controllers, the IndexController that is accessible by everyone and is mostly used to display data and another controller called AdminController only accessible by administrators containing all the actions needed to manage the modules data.


Every module also has a configs folder for module configuration files, like the configuration.xml, the translations.tmx file, the routes.xml file, the acl rules and resources xml file and eventually some other xml files containing the widgets layouts. There is also a main “configs" folder in the application root directory where I store files which have values that can be used website wide by all modules.

Create a default structure

First create the following application folders structure:

|-- application
|   |-- caches
|   |-- configs
|   |-- layouts
|   |   |-- helpers
|   |   `-- scripts
|   |-- logs
|   |-- modules
|   |   |-- admin
|   |   |  |-- configs
|   |   |  |-- controllers
|   |   |  |-- forms
|   |   |  |-- models
|   |   |  |  |-- mongo
|   |   |  |  |-- mysql
|   |   |  |  |-- session
|   |   |  |  `-- xml
|   |   |  `-- views
|   |   |     |-- helpers
|   |   |     `-- scripts
|   |   |-- homepage
|   |   `-- articles
|   |-- services
|   `-- widgets
|-- library
|   |-- Myapp
|   `-- Zend
`-- public
    |-- captcha
    |-- css
    |-- fonts
    |-- images
    |-- javascript
    |-- qrcodes
    `-- uploads

* Myapp is the main project directory.

* The application folder will contain everything related to our app (our php code). We will put all our code one level lower then the “public" directory, to ensure that it isn't "publicly accessible", which is important for security reasons. If you go back to step1 and look at the vhost configuration you will notice that the vhost document root points to the applications "public" directory and not the myApp root.

This means that even if your server is not well configured and allows users to view or download php scripts, they won’t be able to do so as the only files that will be directly accessible, through the browser, will be the files that are in the public folder. But they won't be able to access the files that are below that level, like the files in the application or library directories. The public folder will contain files like your css, javascript and images (you can also rename the public folder to "htaccess" if you prefer). This is the only folder that will be accessible through the browser. In that folder we will put an index.php file that will redirect the requests.

* The library folder is for your library and the zend library

Download the Zend Framework 1 Library and add it to your project

Download the latest version of the Zend Framework 1 and put it into the "library" folder. In the Zend Framework compressed archive file there will be a directory called “Zend", copy that one with everything inside and paste it into your library folder.

This is how myapp directory is build, there are more layouts you could try. More Information about Zend Framework folder layouts can be found in the Zend Framework wiki.

Ok now your ready for the next step, which will be about creating the htaccess file.