Zend Framework Quick Start
Create a Rewrite Rule
Zend Framework's MVC implementation makes use of the Front Controller
pattern. You must therefore rewrite all
incoming requests (except those for static resources, which your application need not handle) to a single
script that will initialize the FrontController and route the request.
If you're using mod_rewrite for the Apache web server, create the file QuickStart/public/.htaccess with
the following contents:
# public/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
This set of rewrite rules specify that if the file exists under the document root directory, it should simply be served as a static resource. Otherwise, the request is for dynamic content and should be rewritten to our index.php script. Since all requests for non-static content will be rewritten to it, the index.php script serves as the entry point to our application.
Some web servers may ignore .htaccess files unless otherwise configured. Make sure that your web server is configured to read the .htaccess file in your public directory.
Let's make sure everything is working so far. Create the file index.php and add the following contents:
<?php
echo 'Hello, Zend Framework!';
Now open your favorite browser and enter the URL http://localhost/ to see a simple page that says "Hello, Zend Framework!".
If this does not work, check your web server logs and make sure you have rewrite enabled with the correct rewrite rules for your web server.
We didn't forget the closing (?>) PHP tag! We intentionally omit it to avoid unintentional output of whitespace in the response in certain cases. In fact, this is one of the best practices recommended in the Zend Framework coding standards.
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.
