Zend FrameWork Part-6

In this tutorial we will study about Zend Framwork of PHP, bootstrapping in ZF, setting up controllers etc. Subsequent pages will discuss on model, view section and the coding of ZF.


Share on Google+Share on Google+

Zend FrameWork Part-6


In this tutorial we will study about Zend Framwork of PHP, bootstrapping in ZF, setting up controllers etc. Subsequent pages will discuss on model, view section and the coding of ZF.

Zend Framework -6:

Bootstrapping Background:

Front Controller design pattern is followed by the Zend Framework. By doing this all request are forced to made through index.php. A file .htaccess is used to achieve this which is created by Zend Tool, it redirects all requests to public/index.php. Location of .htaccess file is <root folder>/public

Entry point to any application is index.php and it is used to create an instance of Zend_Application to run the application. Two constants APPLICATION_PATH and APPLICATION_ENV are included in this file. Function of these constants are to define the path of the application/ directory and the environment. The .htaccess file sets it to development.

The Zend_Application component automatically generates application.ini, which is used to set the configuration of the directives. Zend Application component starts the application also. 

In the ZF a Bootstrap class is used in application/Bootstrap.php, which is the sub class of  Zend_Application_Bootstrap_Bootstrap and it executes any specific start-up code. application/configs folder have application.ini which is loaded using Zend_Config_Ini component.  Zend_Config_Ini is capable of handling OOP features (inheritance in this case) and denotes by using a colon on the section name. For example:

[staging : production]

Meaning of the above coding is that staging section inherits the properties of production settings. APPLICATION_ENV constant is used to select which section is to be loaded. development section is used when developing is being done and the production section is used when the live server is used. All changes are made in application.ini so that all configurations are loaded when the changes we make.

To change the timezone for the application we need to edit the following lines in application/configs/application.ini at the last in the [production] section:

phpSettings.date.timezone = "Europe/London"

or the timezone you want to set.


Application Specific Code:

Zend framework expects to get the pages in organized manner. In ZF the pages are called as action and the group is called as controller, like if the purpose of your website is selling CDs, Books etc then CD, Book will be controllers and pages which describes different items will be actions. ZF's MVC supports the modules of the grouping of controllers.

ZF's controller reserves an action called index.php which is used as default page or action. Suppose the action is present in the CD controller then http://localhost/book_sale/public/CD will execute the index action present in CD controller. There is another index.php file is present which is caused the http://localhost/book_sale to display the default page.

In this present tutorial we will maintain four pages:

  • Home Book
  • Add Book
  • Edit Book
  • Delete Book

Each page has one common Index controller, and functionality of every page is same as it's name, like Home page is for indexing purpose, Add Book page is for adding new books etc.

Setting up the controller:

Now we will set up our controllers, In ZF the controller should be placed within application/controllers/ directory. Controller is a class and the naming convention of the class is that the class must resides in a file called <Controller Name>Controller.php, and the name of the class should be like <Controller Name>Controller and the name of the class should start with a upper case.  Every action in the file should be public and the name of the function should be like <action name>Action. Name of the actions should start with lower case and the name of the action must be in lower case.

Zend_Tool automatically creates the IndexController.php file in this file one default action has been created automatically called indexAction(). To add additional controllers zf command line tool's create action command is used. Open the command prompt and change the directory to book_sale/ or the project name directory and type the following commands:

zf create action add Index

zf create action edit Index

zf create action delete Index

These commands will add three actions in the IndexController.php file.



Posted on: April 13, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles