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 public/index.php
script:
- The
APPLICATION_PATH
constant is defined. This is the full path to theapplication
directory. You’ll see this constant used later. - The
APPLICATION_ENV
constant is defined. This will typically be eitherproduction
,staging
,testing
, ordevelopment
. This will be used to determine which configuration section to use. - The
library
directory is added to the include path. This is so that theZend
library code (if you decide to place it in yourlibrary
directory) or any other libraries used can be found. - A new instance of
Zend_Application
is created. Two arguments are passed to its constructor: the application environment (defined in 2) and the location of the configuration file. Typically this isapplication/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
bootstrap()
orrun()
method inZend_Application
. In fact, this is the essence of the Front Controller pattern.
By itself, 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
production
,staging
,testing
, ordevelopment
mode. - In the demo application, I set the default character set to
utf-8
. - 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.
- Don’t rewrite if the requested file is a regular file with size greater than zero, is a symbolic link, or is a directory. This way static files such as images, CSS files, and JavaScript files aren’t rewritten.
- Otherwise, rewrite the request to
index.php
.
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.
One Comment
Very nice explanation, great work!
One Trackback
[…] Bradley Holt « Front Controller Pattern in Zend Framework […]