Setting up the Zend Framework

by Naneau

This article is very old. I’m keeping it here for legacy reasons, but if you’re looking for help setting up Zend Framework, google is your friend.

I would like to take some time to talk about setting up the Zend Framework. It’s a lot easier than you might think. I know there are quite a few resources out there that deal with getting started, but some of them work with older versions of the framework, or are quite technical and hard to understand. I will try to keep things as easy as possible. There’s a Zend Framework sample file you can use. It uses the directory layout followed in this tutorial. The framework itself isn’t included, though.

To get started, you will need the framework itself. You can download it from Zend. And that’s pretty much all you need to download, because it doesn’t depend on anything. All you need for it to work is a working webserver, and php (any version above 5.1.4). You’ll have to decide where to put it. If you have your own local server, create a subdirectory for it in your web root. If you want to put it on a production server, you really shouldn’t need this tutorial ;) .

In the archive you have downloaded from Zend you will find a few directories under the root directory (the one called ZendFramework-[version]). The only one you really need is ‘library’. It contains the stable components of the framework. You may also want to extract the ‘incubator’ directory, it contains those components that aren’t yet ready for the library.

The question is, where to extract it to? You could put everything, framework and application, in a directory under your web root. If you have shared web hosting, or are just experimenting with the framework, this probably is the best choice for you. The disadvantage of this setup is that you must take some additional steps to make sure that you restrict access to those directories that contain your application and the framework. If you’ve downloaded my sample setup, you should put it under the library directory. Another option is to create a directory away from your webroot and put everything there, and have only the bootstrap file in the web root itself.

I’m going to follow the first option for this tutorial. Not because it’s better, but because it’s easier. I’m going to assume you put everything in a subdirectory of your web root called ‘zf’. Apart from the framework you will need models, views and controllers, and maybe configuration files. You should create subdirectories for them as well, ending up with a directory structure like this:

1
2
3
4
5
6
7
8
/zf/
    application/
        /config/
        /controllers/
        /library/
            /Zend/
        /models/
        /views/

That’s all you need to get started, file-wise. All you need now is a bootstrap file. Bootstrapping gets it’s name from Baron Münchhausen who claimed to have pulled himself out of a swamp by his own boot straps. For the Zend Framework this means you have to create a php file that loads the framework and starts the controller dispatch process, something it can’t do by itself.

The first thing you have to do in this bootstrap file is make sure the include path is set up correctly. You should at the very least include the directory in which you have put the framework, and the directory in which you will store your models. Also, be sure to set a default timezone. Call the file index.php and store it directly under the web root.

1
2
3
4
5
6
set_include_path('.' . PATH_SEPARATOR . './application/library/' .PATH_SEPARATOR . './application/models/');
//include path to zend framework and models

date_default_timezone_set('Europe/Amsterdam');
//timezone and time options
//(I'm dutch, so for me it defaults to Europe/Amsterdam)

Next, you can create an instance of Zend_Controller_Front, and start the dispatch process.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * Zend Front Controller
 */

require_once 'Zend/Controller/Front.php';

$controller = Zend_Controller_Front::getInstance();
//frontcontroller
$controller->setControllerDirectory('./application/controllers/')
//set the directory in which you put your controllers
->setBaseUrl('/zf')
//subdirectory for requests
//set this if you have installed the bootstrap in a subdirectory of your web root
->throwExceptions(true);
//make it throw exceptions (handy for debugging)
//don't do this on a production server

$response = $controller->dispatch();
//do... something!

You need just one little thing now. Your webserver should forward all requests to the bootstrap file. For apache you can do this using mod_rewrite with a .htaccess file. Be sure to include a RewriteBase if you have installed the framework in a subdirectory of the web root. Put it in the web directory.

1
2
3
RewriteEngine on
RewriteBase /zf/
RewriteRule !\.(js|css|ico|gif|jpg|png)$ index.php

Remember that I said you should protect the application directory? Well, to do that you could put the following .htaccess file in there. It tells the web server to deny all direct requests to the directory.

1
deny from all

To get you started, I will provide you with a sample controller here. Put it under the controllers directory, in a file called IndexController.php. You can check if it works by browsing to http://yoursite.com/zf/ assuming you installed it in a ‘zf’ subdirectory. The dispatcher will check for an index controller by default, and call the index action on it, if no other controller and/or action is specified in the url.

1
2
3
4
5
6
7
8
9
10
<?php

require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action {
   
    public function indexAction() {
        echo 'You now have the Zend Framework running! So, hello, world!)';
    }
}

You should now have the following files and directories:

1
2
3
4
5
6
7
8
9
10
11
12
13
/zf/
    application/
        .htaccess
        /config/
        /controllers/
            IndexController.php
        /library/
            /Zend/
        /models/
        /views/

    .htaccess
    index.php

And that’s all there is to it! Of course, there are many more things you can do, especially in the bootstrap file. If you want to set up a database connection, you could do so in there. The same goes for the Zend Registry, a custom url router, and what have you. But this is more than enough to get you started! See my tutorial on the Zend Framework to give you some ideas about what to do with it.

Update

Andries Seutens has a great sample setup on his blog. It’s a good starting point for all users new to the framework.