Zend Framework on MAMP

Zend FrameworkAfter spending much time in OOP PHP (Object Oriented PHP) and choosing Codeigniter as my first PHP framework, I decided to start working with Zend Framwork which is said to be less lightweight but seems to be a well-documented and universal reference.

Zend download (ZF) and local installation on MAMP MAc OSX 10.6.8

I’m using MAMP on Mac OSX 10.6.8. I’ve changed MAMP’s document root (in /Applications/MAMP/htdocs by default) to :

DocumentRoot "/Users/my_name/Sites"

in /Applications/MAMP/conf/apache/httpd.conf

So all my PHP projects are stored in /Users/my_name/Sites/. That’s the directory in which I’ll set up ZF.

First download ZF from http://framework.zend.com/download/latest or follow the “Download Now” link from the homepage. I chose the Zend Framework 1.11.11 Full package for download. Unzip it and copy the uncompressed ZF directory to your local web server : /Users/my_name/Sites/ZF/ in my case.

You’ll have to change your php.ini file to declare the path to ZF’s library, which will be required by the applications you’ll create. So open your php.ini file (/Applications/MAMP/conf/PHPX.X/php.ini or browse the /Applications/MAMP/ directory to find it (PHPX.X stands for your current PHP version)) and change your include_path :

;include_path = “.:/Applications/MAMP/bin/php5.3/lib/php” This line has been commented (use of “;” in front of the line) and replaced by the following:
include_path = “.:/Applications/MAMP/bin/php5.3/lib/php:/Users/olivier/Sites/ZF/library”

Don’t forget to restart MAMP once your include_path has been modified ;)

Create a ZF project : the Terminal magical way

Open a Terminal window found in Applications/Utilitaires/Terminal. Then use the following command to place yourself in MAMP’s Document Root :

cd /Users/my_name/Sites

Then type the following command :

/Users/my_name/Sites/ZF/bin/zf.sh create project my_zend

The ZF shell script will be executed and will generate the directory /Users/my_name/Sites/my_zend/ with the following directory tree :

Zend project directory tree

The application directory contains files structured in the MVC pattern. If you’re not new to frameworks like Codeigniter, you know what it means :

  • Models : will contain files with queries to the database
  • Views : will contain files with Html, Css
  • Controllers : will receive the client’s request, treat it, make calls to models and output data in views

The only directory that should be reachable from an internet client is the public directory. Therefore, we will create a Virtual Host.

Using VirtualHosts in MAMP

The use of virtual hosts can be very useful. Indeed, in the default configuration, reaching our new project can be done through the use of the following url :

http://localhost:8888/my_zend (8888 being the default http port in MAMP)

It is possible to reach the site through the url http://my_zend:8888. I personnally use http://my_zend.local:8888 as a virtualhost in order not to mix local websites with internet websites.

In order to achieve this, you’ll have to modify 2 files ; /private/etc/hosts and /Applications/MAMP/conf/apache/httpd.conf. Those files are important files for which you should save a copy thanks to the following command in Terminal :

cp /private/etc/hosts /private/etc/hosts.bak

and

cp /Applications/MAMP/conf/apache/httpd.conf /Applications/MAMP/conf/apache/httpd.conf.bak

In Terminal, type

sudo nano /private/etc/hosts

You’ll be prompted to type your Mac’s Super User password. and will be able to modify hosts. Use the keyboard arrows to get to the bottom line of the file and type

127.0.0.1 my_zend.local

Then press [CTRL + o] to save the changes.

Open the file /Applications/MAMP/conf/apache/httpd.conf with TextEdit.app and add the following lines at the bottom of the file :

NameVirtualHost my_zend.local
<VirtualHost my_zend.local>
DocumentRoot "/Users/my_name/Sites/my_zend/public"
ServerName my_zend.local
</VirtualHost>

You’ve just created a new virtual host. Simply restart MAMP servers and type http://my_zend.local:8888 in your favourite browser :

Zend Framework installation complete

Have a look at the source code : the <html>, <head> and <body> tags are missing. We’ll activate layout in ZF in order to be able to add the missing tags to all our pages.

Enable layout in ZF

Just open a terminal window and type the path to your ZF project :

cd /Users/my_name/Sites/my_zend/

Then type the following command to enable layout :

/Users/my_name/Sites/ZF/bin/zf.sh enable layout

Notice the /application/layout/ directory structure :

layout directory

The file /application/configs/application.ini has also been changed to point the layout directory.

In order to have a correct XHTML Doctype declaration, we have to initialize the view object because declaring a doctype will call a view. We’ll achieve this in modifying the file /application/configs/application.ini and add the following line at the bottom of the [production] section :

resources.view[] = ""

Then declare the doctype in /application/Bootstrap.php :

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
}

This being done, you can fill the file /application/layouts/scripts/layout.phtml with the following :

<!-- application/layouts/scripts/layout.phtml -->
<?php echo $this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Zend Framework Quickstart Application</title>
<?php echo $this->headLink()->appendStylesheet('/css/global.css') ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
<div id="header" style="background-color: #EEEEEE; height: 30px;">
<div id="header-logo" style="float: left">
<b>ZF Quickstart Application</b>
</div>
<div id="header-navigation" style="float: right">
<a href="<?php echo $this->url(
array('controller'=>'index'),
'default',
true) ?>">Index</a>
</div>
</div>
<?php echo $this->layout()->content ?>
</body>
</html>

The source code of the generated Html pages contains all the necessary tags.

Copy Zend library folder

ZF core libraries are to be found in the downloaded /ZF/library/Zend/. Simply copy the Zend directory and copy it in your project : /Users/my_name/Sites/library/ so that it contains the zend directory.

Webliography

Codeigniter Framework

Zend Framework Homepage

Zend Framework download

Zend Framework in action (Book)

Survive The Deep End (Zend book online)

Zend Tutorial by Rob Allen (3-star tutorial)

Code Igniter, a PHP Framework

Choosing a PHP Framework to develop websites can save you time and efforts. I’m not writing this post to write an exhaustive list of the pros and cons of different PHP frameworks but just want to highlight why I think PHP frameworks are worth trying.

In my very case, it has also led me to better programming practises.

Code Igniter - PHP FrameworkKohana - PHP5 FrameworkZend Framework

The framework I’ve developed entire projects with is Code Igniter (v.1.7.2). I have also had a go with Kohana3 (just for fun) because I was looking for a strict PHP5 lightweight framework. I have not got very deep into Zend Framework because it looks a bit heavyweight for my purposes. However, it is developed by Zend Technologies Ltd., the reference in terms of web-based PHP applications.

If you have never tried a PHP framework and are ready to view how it works in a few minutes, just try one of the official video tutorials on http://codeigniter.com/tutorials/ : strictly astonishing!

At first, I was a bit reluctant to choose a framework and preferred coding my own scripts because I tought starting to use a framework would force me into a practise I could later regret (use of coding rules imposed by the framework itself, efficiency in the long term…). This was my first thought before ever trying one.

After my first Code Igniter experience, I have changed my mind and now think they make my life easier. And even if the framework I have chosen was no longer maintained, I could easily choose another framework to develop my new websites without changing so much my programming practise.

In my opinion, the following features make a good and efficient framework :

  • a PHP 5 framework
  • an MVC (Model-View-Controller) architectural pattern
  • a set of helpers (scripts that save you time for managing urls, or form elements, for example)
  • the possibility to script your own controllers, libraries, helpers
  • a well-documented practise
  • a lightweight (thus fast loading) system

It’s all a question of powers…

PHP5-powered

The power of PHP5 frameworks like Kohana is they’re strictly OOP (Object Oriented Programming), a proof of better programming practises. Code Igniter is partly written in PHP5, helpers including PHP4 functions.

MVC-powered

The power of an MVC architectural pattern is that it allows you to structure your code : your PHP scripts are not mixed anymore with Html and MySql requests as they would be in a home-made script without a framework. In a Model-View Controller pattern,you may define specific folders to hold your controller classes, your models classes and your views. Practically speaking, a web page will call a controller. This controller may define variables or call a model to retrieve data from a database, for example. So, the model will execute the MySql query, get the data from the database and return the result to the Controller. The controller will then pass the results to the view which is like an Html template.

MY_Controller.php : writing your own controllers

A PHP Framework lets you write your own controllers. In Code Igniter, the main controller is in /system/libraries/Controller.php. Code Igniter lets you write your own controller. You should name it /system/application/libraries/MY_Controller.php. This file contains a MY_Controller class that actually extends the main Controller class :

<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
class MY_Controller extends Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array(‘my_html’,'my_url’,'my_date’));
}
}
?>

My personal controller here loads the Main Controller constructor and then loads some helpers. These helpers are not the default CI helpers that can be found in /system/helpers/ but my own rewritten helpers : my_html_helper.php, my_url_helper.php and my_date_helper.php which are copies of the original helpers to which I have added some personal functions.

Scripting websites with Code Igniter has led me to write scripts which are very short, easy to manage and wonderfully efficient.

Webliography

Code Igniter

Code Igniter Video Tutorials

Code Igniter Tutorials

Derek Allard’s tutorials

Kohana