Introduction to Zend Framework – Hello World

Introduction

When I study Zend Framework, I browse a lot for good tutorials in net, but sadly found that there is not much tutorials available and the existing are not good or tough to understand. Thats why I decided to start a simple Zend Framework Tutorial for Zend Framework beginners who are already familiar with PHP and have an idea with MVC Architecture (Model View Controller) . Here I am concentrating more in practical examples than theory. For providing better tutorial, I expect your advices and suggestions.

This tutorial is using the Zend Framework Version – 1.9.3 patch 1.

You can download this from http://www.zend.com/community/downloads


Directory Structure

This is one of the important part for setting up a project using Zend Framework. You can create the directory structure in your own needs once you get a good start with this Framework. Here I am giving a good structure (that I used a lot and found good), which is good for small as well as big projects.

I am going to follow this directory structure in this whole tutorial series.

Here we can call our first Zend project as ‘ HelloWorld ‘ .

Create the following directory structure in your www folder

directory-structure

applications folderIn this directory you can create all the code required to run the application. This is not directly accessed by the web server.

applications / config – In this folder you can put your configuration files like config.ini (This we will discuss later).

applications / models – The database related classes you can put in this folder. Please refer http://en.wikipedia.org/wiki/Model–view–controller#As_a_design_pattern

applications / modules – This contains the different modules like public, admin etc.

applications / modules / public – The public module or front end of the website.

applications / modules / public / controllers – The controller contains the code that makes up the application. (We need different controllers for different modules). Please refer http://en.wikipedia.org/wiki/Model–view–controller#As_a_design_pattern

applications / modules / public / views - All the template files will save under this folder. Default template for Zend framework is ‘.phtml’. Inside views we will create different folders for different controllers (this we will describe later in this tutorial).

css, images, js – as you know this contains the style sheets, images, and javascripts.

library – This folder contains the Zend Library.

.htaccess – This file controls the redirection to index.php.

index.php – The bootstrap file.


Now you have an basic idea about the directory structure we are going to use. Now we can create some thing interested.

Edit your index.php in your favorite editor,

1 <?php
2 //Getting the absolute path of the project

define(‘PATH‘, dirname(__FILE__));

3 //Settign the Zend Library pathset_include_path(

PATH.‘/library‘.PATH_SEPARATOR.get_include_path()

);

4 //Including Zend LoaderClass
require_once(‘Zend/Loader/Autoloader.php’);
5 //Loading the auto loader file.( Including the autoloader.php file)
Zend_Loader_Autoloader
::getInstance();
6 //This loads the Classes dynamically
//(just remember __autoload() in PHP).

$frontController = Zend_Controller_Front::getInstance();
7 //Throwing the exceptions.
$frontController->throwExceptions(true);
8 //Setting the control directory. default means which control need to
//execute while typing the URL or the homepage controller.

$frontController->setControllerDirectory(
array( ‘default’ => PATH.‘/applications/modules/public/controllers’ )
);
9 //run!!
$frontController->dispatch();
10 ?>

Now create a file named IndexController.php in your controllers folder.

<?php
//extending our IndexController to Zend’s action controller
//One thing you need to take care is the spelling of our class ‘I’ and ‘C’ must be capital for file name and class name other wise in windows systems it will work fine but not with in Linux based systems (For example your controller name is photo controller then it must be ‘PhotoController.php’ and class PhotoController extends..

class IndexController extends Zend_Controller_Action {

//Here also in the name of the function ‘A’ must be capital
public function indexAction() {
}
}
?>

Now create a folder index (This folder name must be the name of your controller and in small letters) in your scripts folder. In side this ‘index’ folder create your template file index.phtml with the following content.

<?php

echo ‘Hello World’;

?>

Now edit your .htaccess file and add the two lines given below,

RewriteEngine on
RewriteRule .* index.php

RewriteEngine on

RewriteRule .*        index.php

The final directory structure will be like this,

helloworld-finished

Now run your program using http://localhost/HelloWorld/

As I told, here I have no plan to give you theoretical tutorials but some working examples. You can ask your doubts as comments and i will reply under the comment and to your email ID.


Share/Bookmark