01 July 2009

Looking at the public/index.php file of Zend

Because Zend uses a htaccess file that redirects requests to the index.php file, it is actually the most important file in your application. Luckily, it's also extremely easy to set up because of Zend_Application.

First thing we tell the index file is where is our library. Yay. That big pile of 17.9MB that you need to upload everytime to each client's server:

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

You can include other directories as well if you want — it's basically a comma-separated list of include paths.

Then we define a constant or two. It's important to define your application path, as zend will then refer to your file and structure relatively to that path:

defined('PUBLIC_PATH')
    || define('PUBLIC_PATH', realpath(dirname(__FILE__)));

defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../appcharacter'));

defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

As you can see, I also defined the public path, which makes it easier for me to give the path to the uploads directory when users upload their own files/images. We also define the application environment if it is not already defined. As of Zend v1.8 it is now preferred to define it in the htaccess file.

Now we run Zend_Application:

/** 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();
$application->run();

You can see that Zend_Application is very handy. You require the file once, then you tell the application in which environment you are and then give it the configuration of your application. Because it has the environment you're working in, it pulls the correct info from the application.ini file.

All you need to do is to bootstrap it and run it. And your application is up and away, hehe. Hope this helps, will cover other features in future articles.

Take care!
Tjorriemorrie

No comments:

Post a Comment