Zend_Controller is the heart of Zend Framework's MVC
system. MVC stands for » Model-View-Controller
and is a design pattern targeted at separating application logic
from display logic.
Zend_Controller_Front implements a
Controller pattern, in which all requests are
intercepted by the front controller and dispatched to individual
Action Controllers based on the URL requested.
Zend_Controller system was built with extensibility
in mind, either by subclassing the existing classes, writing new
classes that implement the various interfaces and abstract classes
that form the foundation of the controller family of classes, or
writing plugins or action helpers to augment or manipulate the
functionality of the system.
If you need more in-depth information, see the following sections. If you just want to get up and running quickly, read on.
The first step is to create your file system layout. The typical layout is as follows:
application/ controllers/ IndexController.php models/ views/ scripts/ index/ index.phtml helpers/ filters/ html/ .htaccess index.php
In your web server, point your document root to the
html directory of the above file system layout.
html/.htaccess file above to read as
RewriteEngine on RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
The above rules will route any non-resource (images, stylesheets) requests to the front controller. If there are other extensions you wish to exclude from the front controller (PDFs, text files, etc), add their extensions to the switch, or create your own rewrite rules.
The above rewrite rules are for Apache; for examples of rewrite rules for other web servers, see the router documentation.
The bootstrap file is the page all requests are routed through
html/index.php in this case. Open up
html/index.php in the editor of your choice and add
This will instantiate and dispatch the front controller, which will route requests to action controllers.
Before discussing action controllers, you should first
understand how requests are routed in Zend Framework. By
default, the first segment of a URL path maps to a controller,
and the second to an action. For example, given the URL
/roadmap/components, which will map to the
roadmap and the action
components. If no action is provided, the action
index is assumed, and if no controller is provided,
index is assumed (following the
Apache convention that maps a
Zend_Controller's dispatcher then takes the
controller value and maps it to a class. By default, it
Title-cases the controller name and appends the word
Controller. Thus, in our example above, the
roadmap is mapped to the class
Similarly, the action value is mapped to a method of the
controller class. By default, the value is lower-cased, and the
Action is appended. Thus, in our example
above, the action
componentsAction, and the final method called is
So, moving on, let's now create a default action controller and
action method. As noted earlier, the default controller and
action called are both
index. Open the file
enter the following:
By default, the ViewRenderer action helper is enabled. What this means is that by simply defining an action method and a corresponding view script, you will immediately get content rendered. By default,
Zend_Viewis used as the View layer in the MVC. The
ViewRendererdoes some magic, and uses the controller name (e.g.,
index) and the current action name (e.g.,
index) to determine what template to pull. By default, templates end in the
.phtmlextension, so this means that, in the above example, the template
index/index.phtmlwill be rendered. Additionally, the
ViewRendererautomatically assumes that the directory
viewsat the same level as the controller directory will be the base view directory, and that the actual view scripts will be in the
views/scripts/subdirectory. Thus, the template rendered will be found in
As mentioned in the
previous section, view scripts are found in
application/views/scripts/; the view script for the
default controller and action is in
this file, and type in some HTML:
My first Zend Framework App
By default, the
error handler plugin is registered. This plugin expects
that a controller exists to handle errors. By default, it
ErrorController in the default module
Assuming the already discussed directory layout, this file will go in
application/controllers/ErrorController.php. You will also need to create a view script in
application/views/scripts/error/error.phtml; sample content might look like:
An error occurred
An error occurred; please try again later.
With your first controller and view under your belt, you can now
fire up your browser and browse to the site. Assuming
example.com is your domain, any of the following
URLs will get to the page we've just created:
You're now ready to start creating more controllers and action methods. Congratulations!