Using the Zend Framework – Part 1

Edit: Sorry I never got around to finishing this, there’s only 2 parts of 3.

For the past few years I’ve not really learnt anything new PHP dev-wise, mainly because I’ve been getting on with the task of running a business. Anyway, recently I’ve been bringing myself back up-to-speed with what’s happening at the leading edge of PHP development and the Zend framework caught my eye. Its goal is to “Provide a complete system for developing web applications powered by PHP 5” (a bit like PEAR with all the bits glued together maybe).


The aim of these few (1, 2 or 3, I’ve not decided yet) posts is to document what I find as I play with the framework. So any mistakes that I make will also be documented, which may help you if you’re reading this with the aim of trying it out too. It also means, though, that the way that I do things may not be the ‘right’ way and by the time I’ve finished we’ll probably end up with something which, had we the resources, we could go back an improve upon. In a way I quite like this approach – especially when testing something new out – as it echos the erm, ‘organic’, way of software development on-the-fly.

As will become clear I’m a new at both MVC and the Zend Framework and also have a deep rooted preference for procedural programming but we’ll see how we get on. As I say the purpose of this article is for me to get to grips with the whole web 2.0/MVC/framework malarky and see if it’s all cracked up to be.


The Zend framework website was only launched a few weeks ago and the framework is only in ‘preview’ but they obviously held off on any public until they had a good backlog of packages and some suprisingly good documentation to go with. On top of these I also have found:


Now I need the framework. Luckily I already have a local PHP development platform
so I just open a shell to that and navigate to my development sandbox and checkout a fresh copy from the framework’s public read-only repository into a folder named ZF:

[code]cd ~bealers/www
svn checkout ZF[/code]

I then edit php.ini and restart apache to add the ZF library folder to my include_path (along with PEAR which I’d missed previously):

[code]include_path = “.:/home/bealers/www/ZF/library:/usr/share/php”
/etc/init.d/apache restart[/code]

I can’t be bothered to install a local DNS server so on both my host (windows) and the server I’ve added the following to my hosts file:


For Apache I create a new Virtual Host container; note the re-write entries:

DocumentRoot /home/bealers/www/
ErrorLog /var/log/apache/
CustomLog /var/log/apache/ common
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*(.css|.js|.gif|.png|.jpg|.jpeg)$
RewriteRule ^(/.*)$ /index.php

We’re good to go.

Start coding

I’m ready to start coding and I’ve decided upon a simple blog; it’ll be a good way to get some simple business logic going. In URL/Action terms the blog will be something along the lines of:

/index -> view all
/index/title -> view single
/index/comment/title -> add comment to article
/admin/ -> view list of posts to edit
/admin/add -> add item
/admin/edit/id -> edit item

That’s only two controllers with a limited number of actions, so it should be easy

I’m also going to directly copy how PHP WIKI went about things, namely having an abstract initilisation class and hang everything off that.

For the sake of completeness here’s my starting layout, pretty much the same as the phpwiki stuff:

// htdocs/index.php (nothing new here)

require “Zend.php”;


* initialise
require “../lib/BealersApplicationController.php”;

$controller = Zend_Controller_Front::getInstance();

catch (Zend_Controller_Action_Exception $e )
echo “Fatal error, unable to dispatch the controller.”;


only new thing here so far is the call within __construct()
to a (currently empty) global file

abstract class BealersApplicationController extends Zend_Controller_Action
protected $view;

public function __construct()
$this->view = new Zend_View();

// configuration
require “../etc/”;

$this->view->assign(“title”, “Page title” );
$this->view->assign(“body”, ”

Default body copy

” );
$this->view->assign(“nav”, ”


” );

// run this if the controller is not recognised
public function noRouteAction()
print “Action not recognised”;

// run this if the view is not recognised
public function __call($action, $arguments)
print “View not recognised”;

protected function display() {
print $this->view->render(“template.php”);

[php]// app/controllers/IndexController.php

* URLS take the form /hostname/controller(this file)/view/param1name/param1value/etc/

class IndexController extends BealersApplicationController
public function __construct()

// the default view , think of it as index.php
public function indexAction()
$this->view->assign(“title”,”Home page”);

Home page content




I also have a nearly indentical app/controllers/AdminController.php file.




Entering into my browser gets me a very basic website that does nothing.

That’ll do for part one I think. Total elapsed time, 2ish hours.

Part 2


Leave a Reply

Your email address will not be published. Required fields are marked *