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

applications folder – In 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
The final directory structure will be like this,

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.
Now that you have created an excellent first tutorial, where are your second, third and fourth tutorials for using Zend. This is too good to stop here. You are awesome!
Most of the errors are due to single quotation(‘)
When we copy this code directly and pasting we get errors …
replace ‘ with ‘ (do it manually….)
and for Jim, that error is due to the missing function in indexController
place the following code
public function indexAction()
{
}
thank you
Dear friend,
If you follow the instructions in this tutorial this will work. But this is an older method of Zend. I will add the new tutorial and email you soon.
Thanks
[...] helloword app with zf Hi , I am developing hello world app in zend framework by this article I am getting this problem Warning: require_once(Zend/Loader/Autoloader.php) [...]
favorite
Hi ,
I am developing hello world app in zend framework by this article
I am getting this problem
Warning: require_once(Zend/Loader/Autoloader.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\helloworld\index.php on line 11
Fatal error: require_once() [function.require]: Failed opening required ‘Zend/Loader/Autoloader.php’ (include_path=’C:\wamp\www\helloworld/library;.;C:\php5\pear’) in C:\wamp\www\helloworld\index.php on line 11
where is this autoloader.php i have included the library folder .
if i comment the autoloader class lines and run than i got this error.
.;C:\php5\pear // this is my set_include_path();
Fatal error: Class ‘Zend_Controller_Front’ not found in C:\wamp\www\helloworld\index.php on line 17
Please help me what is the problem.
Thanks
Thanks for this great post . I finally able to run Zend frameworks .
Thank you……
Added to stumble, thanks for the great article….
Maybe you could change the webpage name Introduction to Zend Framework – Hello World | rhythmiclife.com to more generic for your subject you create. I liked the post nevertheless.
Sure I will. Now I am after to setup my new company Rhythmic Web Solutions Will update this soon.
Thanks
Your blog is very useful. Thank you so much for providing plenty of practical information. I will bookmark your ebsiteand will be without doubt coming back. Once again, I appreciate all your work and also providing a lot vital tricks for your readers.
interesting post, pretty much covered it all for me although would of added more content.
Hi!, Very interest angle, we were talking about the same thing at work and found your site very stimulating. So felt compelled to com-ment a appreciative thank you for all your effort. Please keep up the fantastic work your doing!
Awesome, this is exactly what I was looking for! Your article just spared me alot of searching around
It is a awesome start point for a beginner.
This is an excellent tutorial. This make zend simple..
i wanted to know more about zend. Con you give me an example for database operation using model class. As of now you discussed only about hello world. Please update it soon..
great job anish
You have done it once again! Great post!
Thanks for a great post, I never thought of it like that before.
rhythmiclife.com’s done it once again! Amazing read.
great experience, dude! thanks for this great
Articles wow… it’s very wonderful report.
Thank you for another informative blog. Where else could I get that kind of info written in such a perfect way? I have a project that I am just now working on, and I have been on the look out for such information.
I found this web site at yahoo and line this articles. Already bookmarked.
Thank you for your great
content.
thanks, the template that you have and I will visit again when there are new templates and new information on your website
oh, i also have Jim’s porblem, but i fix it.
because of chrome, i copied the code from this page,when i pasted it to my editer i found the code is change.
” ‘ ” change to ” ‘ ”
and i also found the file’s name is index.phtml under applications\modules\public\views\scripts, i wrote as index.php
i hope it is useful for other.
thank this tutorial.
Wonderful site, where did you come up with the information in this article? I’m happy I found it though, ill be checking back soon to see what other articles you have.
Hello, I have been viewing your site for some time now and read most of your entries. Is there any way that I can subscribe so I get updates sent to my email? Regards
Thanks for this useful article.
nice share, good article, very usefull for me…thanks
Hello,
can any one help me, i’m getting Fetal Error..
Fatal error: Uncaught exception ‘Zend_Controller_Dispatcher_Exception’ with message ‘Invalid controller specified (index)’ in C:\wamp\www\HelloWorld\Zend\Controller\Dispatcher\Standard.php:242 Stack trace: #0 C:\wamp\www\HelloWorld\Zend\Controller\Front.php(946): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 C:\wamp\www\HelloWorld\index.php(27): Zend_Controller_Front->dispatch() #2 {main} thrown in C:\wamp\www\HelloWorld\Zend\Controller\Dispatcher\Standard.php on line 242
Hi Phebe,
define(‘PATH‘, dirname(__FILE__));
hope u knw the use of define() => defining a constant.
here we are defining a constant named PATH. this is the path to the project folder.
suppose ur project folder is,
D:/wamp/www/projects/index.php
the above code stores the absoulte path of this project ‘D:/wamp/www/projects/’ and where we need the absolute path to this project folder we use the PATH constant.
So this reduces the chance for getting the path errors.
Hope this will be clear for you now
Thanks,
Anish R
I still do not understand with this :
define(‘PATH‘, dirname(__FILE__));
Hello from Russia!
Can I quote a post in your blog with the link to you?
This is an excellent tutorial. This make zend simple..
i wanted to know more about zend. Con you give me an example for database operation using model class. As of now you discussed only about hello world. Please update it soon..
this is a very good start to frameworks, thanks for sharing.