PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Adding Zend_Application to your application

The basics of Zend_Application are fairly simple:

  • Create an application/Bootstrap.php file, with the class Bootstrap.

  • Create an application/configs/application.ini configuration file with the base configuration necessary for Zend_Application.

  • Modify your public/index.php to utilize Zend_Application.

First, create your Bootstrap class. Create a file, application/Bootstrap.php, with the following contents:

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
}

Now, create your configuration. For this tutorial, we will use an INI style configuration; you may, of course, use an XML, JSON, YAML, or PHP configuration file as well. Create the file application/configs/application.ini, and provide the following contents:

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

[staging : production]

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

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

Now, let's modify your gateway script, public/index.php. If the file does not exist, create it; otherwise, replace it with the following contents:

<?php
// Define path to application directory
defined('APPLICATION_PATH')
    || 
define('APPLICATION_PATH',
              
realpath(dirname(__FILE__) . '/../application'));

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

// Typically, you will also want to add your library/ directory
// to the include_path, particularly if it contains your ZF installed
set_include_path(implode(PATH_SEPARATOR, array(
    
dirname(dirname(__FILE__)) . '/library',
    
get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    
APPLICATION_ENV,
    
APPLICATION_PATH '/configs/application.ini'
);
$application->bootstrap()
            ->
run();

You may note that the application environment constant value looks for an environment variable "APPLICATION_ENV". We recommend setting this in your web server environment. In Apache, you can set this either in your vhost definition, or in your .htaccess file. We recommend the following contents for your public/.htaccess file:

SetEnv APPLICATION_ENV development

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Learn about mod_rewrite

The above rewrite rules allow access to any file under your virtual host's document root. If there are files you do not want exposed in this way, you may want to be more restrictive in your rules. Go to the Apache website to learn more about mod_rewrite.

At this point, you're all set to start taking advantage of Zend_Application.

Zend Framework