Benjamin Tigano Developer at Large

A Basic Zend Framework MVC Application

Zend Framework MVC Application Structure

Zend Framework MVC Application Structure

The structure of almost every ZF application starts out the same. The majority of it relies on the ability to rewrite (in most cases, mod_rewrite with Apache.) I’m yet to find a major hosting company that does NOT support mod_rewrite, so you shouldn’t have any problems here. If you’re just starting out, I recommend using Xampp, or Zend Server (you can get the Community Edition for free, and it’s got Zend Framework built right into it.) Both have MySQL included for when you want to connect your ZF application to a database.

If you use Zend Studio as your IDE, you can create a basic ZF application file structure using Zend_Tool. While that is certainly a fine solution, I usually start out with my own skeleton, and then add in the pieces I need. The basic project I always start out with is attached to the bottom of this page.

The Components

Below is an explanation of each file/directory as it pertains to the application itself:

  • .htaccess - At the root of your web server, you should have an .htaccess file that redirects all non-file requests to your ZF application. Anything that exists as an actual file (like CSS, Javascript, images, etc.) will be served like normal, but all others will be routed to your ZF application.
  • zend-project - This is the root of your project, where all the projects files will live. It can be named whatever you'd like, but if you change it, the .htaccess file will need to be updated (that should be obvious, but I figured I'd point it out anyway.) Within this directory will be your other directories (along with a similar .htaccess file):
    • application
      • Bootstrap.php - This is the file that prepares your application to handle the request. Here you initialize settings, store values you may need during the request, and any other logic that should be performed on a global scale. Right now, all our bootstrap file does is set up the Autoloader.
      • configs/application.ini - This is a standard INI file that stores the settings used by your application. I won’t go into too much detail about what’s in this file to start with, but take a peek and it should be pretty self-explanatory.
      • controllers - This is where your view logic will go. These files will join your models with your views, and populate the placeholders that the view and layout will need.
      • controllers/ErrorController.php - I wanted to mention this controller specifically. Any errors (404, 500, etc.) will be routed to the ErrorController, errorAction. By default, this controller will only display specifics about the error (used for debugging) if your configuration settings are set to development.
      • layouts - This is where you’ll store the general site layout (containing the header, navigation, and footer.) There will be a placeholder for the page content which will be populated by what’s in the view scripts.
      • views - This is the page-specific content, not including the header, footer, and navigation. This is the portion of the page that will change (the most) when navigating to a different controller or action.
    • library - this is where your Zend folder would go - which should contain the Zend Framework. If you have any custom plugins, those would go in here as well. Plugins will be described in later sections.
    • public - this is where your css, js, and images will go. Any resources that shouldn’t be run through the framework should be stored in here. I’ve set-up a folder for each type of resource, but your directory structure in here is entirely up to you. Just make sure you keep it neat!
    • public/index.php - This file is where it all begins. All of your .htaccess files should point here. It sets up your environment, the include path, and creates the Zend_Application based on your configuration file. The very last actions it takes is bootstrapping the application and running it.

Here is the starter project with the bare minimum needed to get you started. unzip and place the .htaccess and zend-project directory in your document root. To save space, I have not included the Zend Framework. You'll need to download it from the Zend website, and extract it to the library folder (described above.) After that, you should be good to go!
Download the Basic Zend Framework Application

Ready for the next section? Click here to learn how to add styles, scripts, meta, and other information!

Fork me on GitHub