I recently gave a Zend Framework Introduction presentation at our local PHP Users Group. I built a demo blogging application called Postr that I used as an example throughout the presentation. There was way too much material to cover in the time available so I plan on writing a series of blog posts, each covering a specific area of Zend Framework. Here is the first (and hopefully not the last!) post in this series based on the presentation and demo application.
Like many other frameworks, Zend Framework provides an implementation of the Front Controller pattern. This means that all HTTP requests can be sent through a centralized point. This allows you to take advantage of Zend Framework's routing and Model-View-Controller (MVC) components, if you so choose. By default this is the
public/index.php file (click to see the source code). Note that in Zend Framework only the
public directory is accessible to website visitors, all of your other directories should be outside of your document root. You'll see several things happening in the
APPLICATION_PATHconstant is defined. This is the full path to the
applicationdirectory. You'll see this constant used later.
APPLICATION_ENVconstant is defined. This will typically be either
development. This will be used to determine which configuration section to use.
librarydirectory is added to the include path. This is so that the
Zendlibrary code (if you decide to place it in your
librarydirectory) or any other libraries used can be found.
- A new instance of
Zend_Applicationis created. Two arguments are passed to its constructor: the application environment (defined in 2) and the location of the configuration file. Typically this is
application/configs/application.ini. The application path (defined in 1) is used to provide the absolute path to the configuration file.
- The application is then bootstrapped and run. It may seem odd to those used to writing PHP scripts, but everything that happens in your application is subsequently called from either the
Zend_Application. In fact, this is the essence of the Front Controller pattern.
public/index.php can't make sure that all HTTP requests go through it. If you're using Apache as your web server then you can use its rewrite module to do this (other web servers have equivalent functionality). This is typically done in
public/.htaccess. There are several things happening in this file:
- First the application environment is set so the application knows if it's running in
- In the demo application, I set the default character set to
- Some environments require you to set a rewrite base. If needed, this is usually the first part of the URI (e.g. just
/if your application is the only thing on the web server or
/postr/if your application is in a subdirectory named "postr").
- Turn on the rewrite engine.
- Otherwise, rewrite the request to
In my next post in this series I plan on taking a look at what happens during the bootstrapping phase of your Zend Framework application.