For this article, you should be familiar with at least the basics of Zend Framework, especially naming conventions. If not, check out the ZF site for beginners tutorials and/or Quickstart.

If you don’t want to use ZF at all, but still need the Star Rating with PHP and Ajax, you can skip ahead to the model part.

Bootstraping Zend Framework

The bootstrap file is (simplified) the file where you set up your ZF environment. Bootstrapping ZF in a non-application environment is nothing more that to merely include the bootstrap file manually.

First step after installing ZF and the folder structure in the previous part is to create the „index.php“ file in your „public“ folder, and a second file in your „public/includes“ folder, named „bootstrap.php“. Open „index.php“ in your favorite PHP editor, and include your bootstrap:

require_once('includes/bootstrap.php');

Open your bootstrap and add the following lines:

<?php
// Define path to application directory
defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', ($_SERVER['DOCUMENT_ROOT'] . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
 || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

set_include_path(implode(PATH_SEPARATOR, array(
 realpath(APPLICATION_PATH . '/../library'),
 get_include_path(),
)));

require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
// Register folder library/Mylib/ for custom classes
$autoloader->registerNamespace('Mylib_');

Most lines are copied from the standard ZF bootstrap. We won’t need the APPLICATION_PATH constant for this… I just like to keep it there , in case I switch to application somewhen in the future 😉 Something to keep in mind is the APPLICATION_ENV. You need to change the „development“ to „production“ when you go live with your scripts – this will be explained in the configuration part.

The important part of the bootstrap is the Zend Autoloader, making sure all ZF components plus your own scripts are found. Nothing magical about it if you know the naming conventions. If not: the autoloader searches for a script by the parts of the class call. If you have a class named „Mylib_Rating_Controller“, Zend fetches it from „library/Mylib/Rating/Controller.php“. The class „Rating_Controller“ won’t be found, because it is not defined in your namespace… unless you put $autoloader->registerNamespace('Rating_') in your bootstrap, that is.

Configuration

Next up is the configuration file, where you configure your database (and cache and about a million other things beyond the scope of this tutorial). Create a folder „configs“ in your „Mylib“ folder, and there a „application.ini“ file. Add the following lines of code:

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
database.adapter = "pdo_mysql"
database.isDefaultTableAdapter = true
database.params.host = ""
database.params.username = ""
database.params.password =
database.params.dbname = ""

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
database.params.host = "localhost"
database.params.username = "root"
database.params.password =
database.params.dbname = "rating"

Each section inherits from the one before, so if you define your default DB adapter in the production section, your development section will use this default adapter as well. For more information on config files, pls. refer to the ZF manual.

Back to bootstraping

Now it’s time to tell ZF where to get the configuration and database options. Add the following lines to your „bootstrap.php“:

$config = new Zend_Config_Ini($_SERVER['DOCUMENT_ROOT'] . '/../library/Mylib/configs/application.ini', APPLICATION_ENV);
$registry = Zend_Registry::getInstance();
$registry->set('config', $config);

$db = Zend_Db::factory($config->database);
Zend_Db_Table::setDefaultAdapter($db);

Now you are ready to do some coding for the actual task at hand… the rating controller 😉

Next up: jQuery Star Rating Rating HTML code