Zend Framework Testing - Day 1 - Getting Started or Trials and Tribulations
by me2nd
Sep
2008
I’m not much of a writer so my posting times do vary. So I have decided to commit this month as Zend Framework awareness month on top of National Hispanic Heritage Month, this does not mean that I will post this in Spanish though ( google translate it if you feel the need to ).
O.K. day one ( getting the framework up and running ).
For this you will need the following:
For Linux ( depending on the distribution )
- AMP stack ( Apache, PHP 5.1.4 or greater, MySQL( although not needed for the framework to work, it will be useful in days to come )).
- For Apache enable mod_rewrite ( if not enabled by default ).
- Copy of the Zend Framework can be downloaded here.
Optional
- Copy of the Zend Framework documentation can be downloaded from here or viewed online here and if you want to keep up with National Hispanic Heritage Month in spanish.
This ( well not a tutorial ) helpful post will be based on the Zend Quickstart. I have changed some of the code in the quickstart to suit my needs and hopefully help others with the same predicament. O.k. Lets begin.
You have your AMP stack all setup and everything is in working order. You have just downloaded The Zend Framework and have decompresed it. This shouls leave you with a folder named ZendFramewok-x.x.x ( at the time of this writing ZendFramewok-1.6.0 ). Lets open the folder “click”. Inside the folder you should find a README.txt, INSTALL.txt, a demos directory, an externals directory, a laboratory directory, a library directory and a tests directory.
Now the way I have the Zend Framework setup on my computer might differ from the Quickstart, this is because I like absolute paths in my php code.
My web root on my computer is located in /var/www/html and ServerName is localhost, so http://localhost/ will bring me to the /var/www/html directory. I usually chown the html directory too ( as root user: chown your_user_name /var/www/html ), as I do not use my box as a public web-server.
Let’s start with the directory structure.
* edit: 09/06/2008 reason: can use library as a directory for different php frameworks
* original was:
*->phpframeworks
*-gt;->library
*->->Zend
* will be totally updated in the day 3,4,5 for use with virtualhosts
file structure of /var/www/
->library
->->Zend
->aplications
->->models
->->views
->->->scripts
->->controllers
->html
->->zendtest
As you can see I have 3 folders in my /var/www directory that will be the focus of testing ( library: which can hold many different frameworks, application: another on I also put their and changed the ownership to my desktop_user_name, and html: which is their by default ) . In the applications folder I have three directories models, views, controllers, and a subdirectory of views called scripts, and of course in the Html directory I have the directory zendtest. Which can be found at http://localhost/zendtest.
Creating the simple rewrite rule for your .htaccess file located in the zendtest directory. This code can be found at Create a Rewrite Rule on the Zend Framework website. Create a text file named .htaccess in the zendframework directory. pen it up with your editor and put this in it
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
Now creating the bootstrap index.php file for the zendtest directory. If you look on their website here it shows you the code to create the necessary bootstrap file. Here are the modifications I made.
<?php
// Step 1: Set a flag indicating setup is necessary
$bootstrap = true;
// Step 2: Setup PHP environment
// In this case, we will setup error reporting, but any ini_set or
// environment-related directives should go here. This way, you can create a
// separate PHP environment for running tests.
error_reporting(E_ALL | E_STRICT);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
// define location of parent directory as ROOTPATH
define('ROOTPATH', '/var/www');
// Step 3: Perform application-specific setup
// This allows you to setup the MVC environment to utilize. Later you can re-use this file for testing your applications
require ROOTPATH.'/application/bootstrap.php';
// Step 4: Dispatch the request using the front controller.
// The front controller is a singleton, and should be setup by now. We will grab
// an instance and dispatch it, which dispatches your application.
Zend_Controller_Front::getInstance()->dispatch();
If you have line numbers enabled in your editor you will see on lines 13 and 14 I have inserted
// define location of parent directory as ROOTPATH
define('ROOTPATH', '/var/www');
So instaed of a relative path we have an absolute path so all you have to do is add ROOTPATH instaed of ../ on line 17.
require ROOTPATH.'/application/bootstrap.php';
I did this because going back one directory will not work if we have our library in the phpframeworks directory. Well you could always use ../../, but like I said I really do not like relative paths.
Now that is finished. Let’s move into the applications directory and make the bootstrap.php file. Here is the one I use
// Step 1: Check to see if the application environment is already setup
if (isset($bootstrap) && $bootstrap) {
// Step 1a: Add our {{library}} directory to the include path so that PHP can find the Zend Framework classes.
set_include_path( ROOTPATH . '/library' . PATH_SEPARATOR . get_include_path());
// Step 1b: Set up autoload.
// This is a nifty trick that allows ZF to load classes automatically so that you don't have to litter your
// code with 'include' or 'require' statements.
require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();
}
// Step 2: Get the front controller.
// The Zend_Front_Controller class implements the Singleton pattern, which is a
// design pattern used to ensure there is only one instance of
// Zend_Front_Controller created on each request.
$frontController = Zend_Controller_Front::getInstance();
// Step 3: Point the front controller to your action controller directory.
$frontController->setControllerDirectory(ROOTPATH .'/application/controllers');
// Step 4: Set the current environment
// Set a variable in the front controller indicating the current environment --
// commonly one of development, staging, testing, production, but wholly
// dependent on your organization and site's needs.
$frontController->setParam('env', 'development');
The only real difference between the one on the website and mine is that on line 6 and 22 I added the ROOTPATH constant. Since it is already defined in our /var/www/html/zendtest/index.php file
Since I am just starting with the Zend Framework ( long time listener first time caller ) this may not be the correct Zend compliant way of doing this , but it works for me and should work for you too. If you see any error’s in this post, please feel free to leave a comment because at H8ersclub comments have no strings attached.

