Zend FrameWork Part-4


 

Zend FrameWork Part-4

In this tutorial we will study about Zend Framwork of PHP, how to bootstrap (Zend Framework (V-0.6)) using index.php file, you will get the detail description of the file in this tutorial. This is the first episode of the series subsequent pages will discuss on other and advance topic.

In this tutorial we will study about Zend Framwork of PHP, how to bootstrap (Zend Framework (V-0.6)) using index.php file, you will get the detail description of the file in this tutorial. This is the first episode of the series subsequent pages will discuss on other and advance topic.

Note: The following example is based on Zend Framework 0.6 version, the following code may not work with the latest or any other version of Zend Framework.....Tutorial based on ZF 1.10 will be published soon.

Zend Framework Part-4:

Note: The following example is based on Zend Framework 0.6 version, the following code may not work with the latest or any other version of Zend Framework.....Tutorial based on ZF 1.10 will be published soon.

Put the following code inside index.php:

<?php

error_reporting(E_ALL|E_STRICT);

date_default_timezone_set('Asia/Kolkata');

set_include_path('.' . PATH_SEPARATOR . './library'

. PATH_SEPARATOR . './application/models/'

. PATH_SEPARATOR . get_include_path());

include "Zend.php";

include "/Zend/Controller/Front.php";

Zend::loadClass('Zend_Controller_Front');

$baseUrl = substr($_SERVER['PHP_SELF'], 0,

strpos($_SERVER['PHP_SELF'], '/index.php'));

$fC = Zend_Controller_Front::getInstance();

$fC->setBaseUrl($baseUrl);

$fC->setControllerDirectory('./application/controllers');

$fC->throwExceptions(true);

$fC->dispatch();

?>

 

Let us discuss the coding mentioned above line by line:

error_reporting(E_ALL|E_STRICT);

date_default_timezone_set('Asia/Kolkata');

With the above line we can display any errors that we could make, please check your php.ini settings and set the display_errors set to on. We can get our desired time zone by writing the following code:

date_default_timezone_set('<your desired time zone>');

Change the time zone to your desired time place, please check the manual to select your desired time zone.

In Zend Framework the design is built in such a manner that we need to include the path of the files. Place the models directory on the include path so that we can  load the model classes anytime after initializing the project development.

0

To start we have to include the Zend.php to access the Zend class present in ZF.

Zend::loadClass('Zend_Controller_Front');

Basic functionality of Zend::loadClass is to load the class. The loading of the class could be achieved by converting the underscores mentioned above in the class name to path separators and then adding .php to the end of the line. This is how the class Zend _Controller_Front will be loaded from the file Zend/Controller/Front.php. If you follow the naming convention mentioned above, you can use
Zend::loadClass() to load the classes.

A  "router" class is being used by the front controller to map the requested URL to connect with exact PHP function for displaying the page. We can set the base URL with the help of setBaseUrl().

1

Now set the URL (base) to the correct URI to our index.php. For practicing purpose you may set this to $_SERVER['PHP_SELF'], for other or real projects assign $baseUrl to the correct or desired URL in index.php.

$baseUrl = substr($_SERVER['PHP_SELF'], 0,

strpos($_SERVER['PHP_SELF'], '/index.php'));

2

$fC = Zend_Controller_Front::getInstance();

$fC->setBaseUrl($baseUrl);

Now we need to configure the front controller so that it will come to know about the existence and the directory structure :

3

$fC->setControllerDirectory('./application/controllers');

The front controller catch the exceptions and store them in _exceptions field of the Response object.Response object holds the required info of the response to the requested URL along with the additional information like header, page content etc. Front controller send the headers automatically and displays the content of a web page before the completion of the work. For practicing purpose it is ok to rethrow the exceptions so it will be visible but in reality no exception message should be displayed on the web page.

$fC->throwExceptions(true);

4

To run the application execute the following code:

$fC->dispatch();

Till now we are not prepared yet to run the application, so please keep patience and in the next tutorial we will discuss about how to design the database and all.

5

 

Ads