I’ve started playing with the Zend Framework and noticed that there were lots of getting started tutorials for the old versions. Since many of them don’t update or work anymore I’ve decided to write my own getting started version which should get you up to speed with Zend Framework version 1.0.1 in no time.
At first you need to download and install the framework in a seperate directory, you can either choose that it’s available in the include_path using the php.ini file or by setting it up in a folder which will be used only for your application. If you go for the first one edit the php.ini and search for the include_path directive, add the complete path to the library directory from the extracted Zend Framework archive and you should be set, don’t forget to reload Apache!
The second method is slightly harder but it should also be possible when you’re on a shared hosting platform and the provider hasn’t got the framework installed. Extract the archive somewhere outside the web root (this means that it shouldn’t be accessible through the website).
When you set up a new project, the following directory structure is recommended:
- app
- controllers
- views
- scripts
- library (contains the Zend Framework files)
- public_html (or htdocs in other cases - this is the web root)
In the public_html directory create a file called index.php. This will be our bootstrap for the framework. But first we need to get the PHP error reporting to a decent level and make sure to have access to the framework files. Open index.php and add the following;
<?php
/**
* Error reporting
*/
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
define('ZEND_PATH', '../library');
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . ZEND_PATH);
Lines 8 and 9 are only needed when you don’t have access to the Zend Framework and you’ve installed it on a separate location. Line 9 adds the defined path to the include_path so the shared PHP libraries are still accessible.
It’s wise to add a version check to your application, if the framework is outdated the user would know and should update. This way you’ll never get weird results when someone is using an older version than the application was designed for. After line 9 add the following block.
/**
* Check correct Zend version
*/
require 'Zend/Version.php';
if(Zend_Version::compareVersion('1.0.1') > 0)
{
die('The current version of the Zend Framework (' . Zend_Version::VERSION . ') is outdated. Please update!');
}
Now, if a user has a version lower than 1.0.1, the only thing they’ll see is the message ‘The current version of the Zend Framework (x.x.x) is outdated. Please update!’. The method returns -1 if the specified $version is older than the Zend Framework version, 0 if they are the same, and +1 if the specified $version is newer. You can test if this works by changing the version numer on line 15 to some version that doesn’t even exist yet, if all goes well you see the error.
Now to the good part, adding controllers. For this you need the Zend Loader and the Zend Controller Front script. Zend Loader is merely used to replace the require() or include() functions, it’ll display an error if something goes wrong and should be used instead of those two! To setup the controller dispatcher add the following code:
/**
* Zend Loader
*/
require 'Zend/Loader.php';
/**
* Required Classes
*/
Zend_Loader::loadClass('Zend_Controller_Front');
/**
* Setup Controller
*/
$front = Zend_Controller_Front::getInstance();
$front->setControllerDirectory('../app/controllers');
$front->throwExceptions(true);
Before we run any controller we first need to tell the framework where to look using the setControllerDirectory() method. It looks in a few standard paths at first but we overrule it since we don’t want them to be accessible directly. We also want all errors and exceptions on screen instead of the error controller while we are developing so we call the throwExceptions() method and assign the value true to it.
Now the last thing we need is to actually run the controller by issuing the dispatch() method. Use the following code and run it in a browser and see what it does.
/** * Dispatch it */ $front->dispatch(); ?>
If all goes well you get the following error message Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (index)'. This basically means that it can’t find the Index controller, which is correct since we haven’t created that yet. Let start doing that now. Add a file called IndexController.php in the app/controllers directory and put the following code in it.
<?php
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
srand();
$this->view->assign('randomNumber', rand(1, 10));
}
}
?>
When nothing is supplied in the URL, Zend Framework uses the index controller and the indexAction() method to display as the default. When you create your controller, you need to extend it with the Zend_Controller_Action class so you have all the basic functions available. One of those is the assign() method from the view class. In this example we create a view (template) variable called randomNumber and assign a random value from 1 to 10 to it. The only thing we still need to do is to create the view. To create a view create a directory in the apps/views/scripts directory named index. In this directory all views will be created per action, so for this instance we create the view index.phtml (notice the different file extension). Open the newly created file and put the following HTML code in it.
<html> <head> <title>Index Controller</title> </head> <body> <p> This is the index action of the index controller<br /> The randomly generated number is: <?= $this->randomNumber ?> </p> </body> </html>
If you visit your site now, you’ll get the message and the randomly generated number which will change each request.
When you go to a controller or action that doesn’t exist, you’ll still get an error message. You can change this into a nicer once by creating an error controller in the controller directory and creating the error view. For example:
Error controller (controller/ErrorController.php):
<?php
class ErrorController extends Zend_Controller_Action
{
function errorAction()
{
header('HTTP/1.0 404 Not Found');
}
}
?>
Error view (views/scripts/error/error.phtml):
<html> <head> <title>Oops!</title> </head> <body> <h1>Oops! 404</h1> <p> You appear to have made a mistake by going to a section of the site I can't find. Please go <a href="/">back to the main site</a> and continue your search for fortune from there! </p> <hr /> <address><?= $_SERVER['SERVER_SIGNATURE'] ?></address> </body> </html>
Don’t forget to edit the public_html/index.php on line 34. Either comment out that line or change the variable to false. Request an unexisting page and you should see the home made 404 page.
November 27th, 2007 on 09:34
for the errorController to work, don’t you need to add the .htaccess file with mod_rewrite?
November 28th, 2007 on 23:35
The error controller will work as long as you use URLs like: http://example.com/index.php/news (note the index.php)
If you don’t want the index.php in your URL, you should setup a global .htaccess (or edit it in the Apache main config file) and indeed use the mod_rewrite functionality.
An example .htaccess could be:
December 20th, 2007 on 01:13
Thank you!
I’ve tried the IBM’s “Understanding the Zend Framework” article, and it’s too outdated.
June 9th, 2008 on 20:33
DATED. ZF 1.5 is now current version. this tutorial doesn’t work when followed verbatim
I Get
=========
Fatal error: Uncaught exception ‘Zend_Controller_Dispatcher_Exception’ with message ‘Invalid controller specified (index)’ in C:\wamp\www\mysite\library\Zend\Controller\Dispatcher\Standard.php:24
June 10th, 2008 on 06:38
Tutorial still works with the latest (1.5.2) version. You must have missed a step.
July 9th, 2008 on 00:08
The tutorial works when the .htaccess file is not used. Does anyone know why I got the same exception as Dark1?
September 17th, 2008 on 18:17
To avoid an error, U might wanna change
$front->setControllerDirectory(‘../app/controllers’);
to a full path like
$front->setControllerDirectory(‘D:\Inetpub\wwwroot\Zend\Test\apps\controllers’);
since the tutorial is calling from a relative dir.
I had to comment the set_include_path
December 4th, 2008 on 17:14
dont forget to omit the closing php tag ?> if the file only contains php
October 8th, 2009 on 16:43
A closing ?> really isn’t necessary