Zend Framework Quick Start
Create an Action Controller & View
In Zend Framework's MVC implementation, both the default action controller and the default action are named 'index'. You should therefore set up the default action controller by creating application/controllers/IndexController.php with the following contents:
<?php
// application/controllers/IndexController.php
/**
* IndexController is the default controller for this application
*
* Notice that we do not have to require 'Zend/Controller/Action.php', this
* is because our application is using "autoloading" in the bootstrap.
*
* @see http://framework.zend.com/manual/en/zend.loader.html#zend.loader.load.autoload
*/
class IndexController extends Zend_Controller_Action
{
/**
* The "index" action is the default action for all controllers. This
* will be the landing page of your application.
*
* Assuming the default route and default router, this action is dispatched
* via the following urls:
* /
* /index/
* /index/index
*
* @return void
*/
public function indexAction()
{
/*
There is nothing inside this action, but it will still attempt to
render a view. This is because by default, the front controller
uses the ViewRenderer action helper to handle auto rendering
In the MVC grand scheme of things, the ViewRenderer allows us to
draw the line between the C and V in the MVC. Also note this action
helper is optional, but on default.
*/
}
}
Because we're using the standard router, requests for http://localhost will be routed to the indexAction method on the IndexController action controller, as will requests to http://localhost/index and http://localhost/index/index.
To work correctly with the standard router, action method names should follow the lowerCamelCase convention with the suffix 'Action'. Action controller classes, on the other hand, should follow the UpperCamelCase convention with the suffix 'Controller'. While the standard router should work for almost all applications, routing in Zend Framework is completely customizable. See the Reference Guide for details.
It may seem like indexAction() is doing nothing at all, but again Zend Framework is doing useful things for you behind the scenes. In this case it is routing the request to the appropriate view to return the correct response. Unless otherwise configured, the view it will look for is views/scripts/index/index.phtml, where the views directory is contained in the same folder as the controllers directory you passed to Zend_Controller_Front::run(). The file extension for views is yet another best practice recommended for Zend Framework applications; since view templates look suspiciously like PHP scripts in ZF, it is easier to distinguish view templates from other PHP files if you use the .phtml extension instead of .php.
Let's create the view script now with the following contents:
<? // application/views/scripts/index/index.phtml ?>
<h1 align="center">
Hello, Zend Framework MVC!
</h1>
Now go to http://localhost; you should see 'Hello, Zend Framework MVC!' with very basic formatting. Like this:
If you do not see this message, check your server logs and make sure you've created the specified files with the exact contents from above.
Although ZF has its own template engine that renders templates using a syntax very similar to standard PHP, you can easily configure Zend Framework to use other template engines such as Smarty or PHPTAL. See the reference guide for details.
Quickstart Navigation
- ZF & MVC Introduction
- Set Up the Project Structure
- Download & Install ZF
- Create a Rewrite Rule
- Create a Bootstrap File
- Create an Action Controller & View
- Create an Error Controller & View
- Create a Layout
- Create a Configuration and Registry
- Create a Model and Database Table
- Create a Form
- Congratulations
ZF Reference Guide - now in PDF!
The Zend Framework Reference Guide is now available in PDF format from Zend's
high-speed content distribution network. Registration is required.
Documentation Archives
If you're looking for an older version of our reference guide, you'll find it in our download archives.
