Zend: PHP-based web framework

Saturday 19th of September 2009 10:32:54 AM

  Toggle Advanced Options

Setting up the Zend Framework on Ubuntu - Part One

First of all, you have to install the appropriate packages with Synaptic Package Manager. Let’s start with installing PHP5 (the Zend Framework only works with PHP 5.13 or PHP 5.14 and above) and all its dependent libraries. The next package to install is the PHP5-MySQL library which includes both the ‘pdo_mysql’ and the ‘mysqli’ modules. In my case, I’m interested in the PDO-module because the topic map store (used in QueSucede.com) is implemented on top of the PHP Data Objects (PDO) extension.

Right, that was the easy part. The tricky part is the Apache configuration which is not difficult but you can’t skip these few essential steps otherwise the Zend Framework will just not work. So, what is the Apache stuff all about? Well, in two words: URL rewriting. Those two words are enough to strike fear into the heart of any intrepid developer ;-)

Okay, just kidding. In Ubuntu, it is relatively simple to enable Apache URL rewriting. First of all, open a terminal and change directory to ‘/etc/apache2′. Within this directory you’ll see the two directories that we are interested in, namely ‘mods-enabled’ and ’sites-enabled’. The mods-enabled directory contains a bunch of symbolic links to (Apache) configuration files/directives that Apache uses to load modules when it (re)starts. So all we have to do is create the symbolic link to the appropriate directive (in our case ‘rewrite.load’ ) by executing the following line in the terminal window (from within the mods-enabled directory):

ln -s ../mods-available/rewrite.load rewrite.load

Once that is done, we change to the previously-mentioned ’sites-enabled’ directory and load the ‘000-default’ file (a symbolic link to /etc/apache2/sites-available/default) into a text-editor and change the following: ‘AllowOverride None’ to ‘AllowOverride All’ in both the and sections. Once that is done, save the file and open up the ‘Services Admin’ window and restart Apache (Web server). That’s it. URL rewriting should be enabled in Apache.

Setting up the Zend Framework on Ubuntu - Part Two

The next step involves creating the appropriate directory layout as shown in the image below. The image exemplifies the standard directory structure as used by the Zend Framework.

Zend framework directory structure

Okay, let’s start: open a terminal window and change the current directory to ‘/var/www’. The /var/www/ directory is Apache’s DocumentRoot directory, i.e., the top-level directory for Apache and it is here that we are going to create the before-mentioned directory structure. I normally start with ‘sudo mkdir –p application/views/scripts’ (the ‘-p’ parameter means ‘parent’ and will create the necessary parent directories if needed, it just means less typing for you) followed by ‘sudo mkdir –p public/styles’. Afterwards, I just ‘cd’ into each directory and create the remaining directories. Remember, we have to use ‘sudo’ because the /var/www directory belongs to root. The first time around the sudo-command will ask you for your password, but after that (although there is a timeout) it is not required.

Once we have created the above-mentioned directory structure it is time to take care of a few house-keeping tasks for Apache. That is, we have to create a couple of, so-called, ‘.htaccess’ files for both URL-rewriting and security purposes. Let’s start with the first one in /var/www; enter the following command in the terminal window: ‘sudo touch .htaccess’ (note the dot before the ‘htaccess’ signifying that this is a hidden file). Once that’s done open a text editor (‘gedit’ is fine) from the terminal window with ‘sudo gedit’. Now, because ‘.htaccess’ is a hidden file, in the ‘Open Files’ dialog you will have to right-click (in the files listing panel) and select the ‘Show Hidden Files’ option. Next open the .htaccess file and enter the following:

RewriteEngine on
RewriteRule .* index.php
php_flag magic_quotes_gpc off
php_flag register_globals off

It’s the first two lines that are the important ones for the moment. Basically, we are telling Apache to turn on its rewrite engine and redirect all requests to ‘index.php’. The third and fourth lines are PHP-directives that could have already been set in the global ‘php.ini’ file. However, in a shared-hosting environment, you would normally not have access to this file, hence the inclusion of these directives in the .htaccess file. On to the next one; change into the ‘application’ directory and create an .htaccess file (following the process outlined above) with the following line in it: ‘deny from all’. This .htaccess file will (surprise, surprise) deny access to this directory (and all its sub-directories) to anyone other than the actual PHP-process. This is done for security reasons; you don’t want anyone looking at your code do you? Next, we do the same in the ‘library’ directory (again, with ‘deny from all’). The final .htaccess file is for the ‘public’ directory. The content of this file is: ‘RewriteEngine off’. We are effectively telling Apache to perform no rewriting whatsoever in this directory and its sub-directories.

Change back to the /var/www directory and create a file called ‘index.php’. This file plays a very prominent role; it’s the front-controller and subsequently the target of the first .htaccess file that we created (with the rule: ‘RewriteRule .* index.php’). All the requests made by your application will be re-directed to this file that will subsequently route them on to their appropriate destination. It does this by splitting the URL into pieces and determining which controller and action should be executed (but this is a discussion for another time). The ‘index.php’ file should contain the following code:

// initial configuration
set_include_path('.' . PATH_SEPARATOR . './library'
. PATH_SEPARATOR . './application/models/'
. PATH_SEPARATOR . get_include_path());
include "Zend/Loader.php";
// setup controller
$frontController = Zend_Controller_Front::getInstance();
// run!

Let’s briefly look at what the above code accomplishes. The first part sets up error reporting (always handy when developing ;-) but don’t forget to switch it off when the app goes live), the time zone (mine is Atlantic/Canary, adjust accordingly) and PHP’s include path.

Next, it includes ‘Zend/Loader.php’ (a utility class provided by the framework to make loading other classes easier) and loads the ‘Zend_Controller_Front’ class followed by getting a front controller instance and setting some options for the front controller. Finally, it runs (the proper term is ‘to dispatch’) the front controller. We will examine this file in a more detailed fashion in a future blog entry. For the moment, however, this should suffice.

Don’t despair. We’re almost there. Change to the ‘/application/controllers’ directory and create the ‘IndexController.php’ file with the following contents:

require_once 'Zend/Controller/Action.php';
class IndexController extends Zend_Controller_Action {
    function indexAction() {
        $this->view->title = "Hello World!";

The functionality of the above file can be summarized as follows: first the ‘Zend/Controller/Action.php’ file is loaded. This is necessary so that the ‘IndexController’ class can extend the ‘Zend_Controller_Action’ class. Furthermore, one action is defined (‘indexAction’) and the string ‘Hello World!’ is assigned to the view title (‘$this->view->title’). Basically, what we are doing here is preparing the necessary objects for the appropriate view. That is, each action basically maps to its corresponding view (unless we override this default behaviour). For example, an action called ’showAction’ in a controller called ‘Topic’ will map to the view ’show.phtml’ in the ‘/application/views/scripts/topic’ directory. Furthermore, objects that are going to be used in the view (displayed, iterated over, etcetera) must be prepared in the action and subsequently assigned to the view object (like we have done with the title in the action shown above).

Right, the final step: change to the ‘/application/views/scripts/index’ directory and create an ‘index.phtml’ file, with the following contents:

<title>Zend framework test</title>
<h1><?= escape($this->title); ?></h1>

This view will render an HTML-page with the title (as assigned in the ‘indexAction’) inserted in the H1-tags. Right, the moment of truth: open up your browser and point it at ‘http://localhost’. If everything went well, you should see a blank page with a “Hello World!” header.


Please take note@09-01-03 21:32:04 by Brett Kromkamp

The above text is copied verbatim from my old (now, non-existent) blog.