Pages

Friday, August 2, 2013

Making the Structure of the ZendFramework Project

Create two directories in the root of the application:

application – here all our software modules of the project will be stored
public -- where will be available all share files.
Also create index.php and .htaccess in the root, in which immediately add rules of redirects:

RewriteEngine on
RewriteRule .* index.php

It’s necessary to add similar .htaccess file, in the folder “public”.

Now lets create 3 folders in “application” directory: configs, library, modules.

configs — here is configuration files of the project.
library — for additional libraries.
modules — and it’s for modules of our application.



After all these simple manipulations I had such a structure:

The root:
application/
configs/
library/
modules/
public/
.htaccess
.htaccess
index.php

If the structure is ready, so we can move on to the coding :

Open our index file (index.php). Let the interpreter know that php code wiil start soon and determine 4 constants:

PATH_TO_ZF - the path to ZF
PATH_TO_APPLICATION - the path to the folder “application”
PATH_TO_LIBRARY - the path to our libraries
PATH_TO_MODULES - the path to our modules.

The code:

define('PATH_TO_ZF', '../../../ZF/1.7.7/');

define('PATH_TO_APPLICATION', './application/');

define('PATH_TO_LIBRARY', PATH_TO_APPLICATION . 'library/');

define('PATH_TO_MODULES', PATH_TO_APPLICATION . 'modules/');

Now we should tell our interpreter the path to load all our stuff from:

include_path(PATH_TO_ZF . PATH_SEPARATOR . PATH_TO_APPLICATION . PATH_SEPARATOR . PATH_TO_LIBRARY);

The next step is to download Zend_Loader (later we will return to it) and register classes autoload.

require_once 'Zend/Loader.php';

Zend_Loader::registerAutoload();

So, Zend_Loader is loaded, now let’s activate Zend_Controller_Front (about it, too, later) and point the location of our modules to the dispatcher. Then start the process of dispatching.

$controller = Zend_Controller_Front::getInstance();

$controller->addModuleDirectory(PATH_TO_MODULES)->dispatch();

The result should be something like this:

define('PATH_TO_ZF', '../../../ZF/1.7.7/');

define('PATH_TO_APPLICATION', './application/');

define('PATH_TO_LIBRARY', PATH_TO_APPLICATION . 'library/');

define('PATH_TO_MODULES', PATH_TO_APPLICATION . 'modules/');

set_include_path(PATH_TO_ZF . PATH_SEPARATOR . PATH_TO_APPLICATION . PATH_SEPARATOR . PATH_TO_LIBRARY);

require_once 'Zend/Loader.php';

Zend_Loader::registerAutoload();

$controller = Zend_Controller_Front::getInstance();

$controller->addModuleDirectory(PATH_TO_MODULES)->dispatch();

As you can notice Zend_Controller_Front never loaded because Zend_Loader loadsController automatically. Zend_Loader recognizes location of the controller on its name:
Zend_Controller_Front is in Zend/Controller/Front.php


No comments:

Post a Comment