Page 1 of 1

Introduction to Zend Framework Creating your first Application with ZF, start to finish Rate Topic: ***** 2 Votes

#1 joeyadms   User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 178
  • Joined: 04-May 08

Post icon  Posted 09 June 2008 - 12:53 PM

Introduction to the Zend Framework Part 1

I will not say that ZF (or the Zend Framework) is the best framework. It all depends on what your style is,
and what you are comfortable in. My choice of Framework, however, is the ZF.

I will take you through downloading, installing, and creating an application cleanly, as well as some extras such
as using databases and layouts, as well as a touch on using a modular directory structure.

Tell me about ZF
Zend Framework is a PHP Framework designed for creating applications in a standardized, modular, and easy to maintain format. It
includes helpers for everything from Ajax, PDF generation, Web Service API's such as yahoo and Amazon, to HTML form generation.

ZF has also recently partnered with Dojo, an AJAX toolkit, and will be shipping soon with it, which is very exciting news.

Unlike other frameworks, take it good or bad, ZF is coded in PHP5, harnessing all the power that the new release has to offer. ZF is backed by
the Zend Corporation, a HUGE name in PHP, and a core contributor to PHP itself, and arguably the reason PHP is making so much progress in the enterprise
department. They offer (at a cost) support for ZF, as well as training, However their is an amazingly gigantic community of people to provide support for

Zend Framework. You should familiarize yourself with the Zend Dev Zone to learn new things.

The great thing about ZF is that it has a use-at-will architecture. Meaning, ZF does not have to "power" your application, you can simply
use some of the features that it has alone, or even plug it into existing frameworks such as CakePHP. This is important also in a Security sense, since
all of your application files remain "beind the wall" (or outside of your public directory), and ZF code does not explicitly handle your data, that means
if a vulnerability was found in the software, you would be protected. ZF is really just a set of connected libraries to help you create your application, and

is not an application itself.

Another outstanding feature of ZF is extendability. Virtually EVERYTHING in ZF is extendable. With other frameworks you can add custom helpers, plugins,

etc.. also, however with ZF you can create your own classes that extend CORE features in ZF, and then plug them in at runtime. This is another sense of you

taking complete control, and tailoring your application to your own uses.

Although you do not have to use it, ZF runs natively on an MVC architecture. There may be debate on wether MVC is best, but I can assure you that it cleans
your code, reduces debugging time, and makes it extremely modular.

MVC? What is this
MVC Stands for Model, View, Controller , it is the theory that you should seperate your business data (Model aka Database), your programming logic
(Controllers), and your presentation logic (Views).

What this means is that, Your controller will handle all your normal manipulation and logic, and contact the model to pull information, then the views will
take the data from the controller and make it presentable to the browser.

In lamens, you seperate your data access logic(SQL Queries etc.) from your normal programming logic (manipulating arrays, authentication, setting variables

etc) from your presentation logic (cycling through and echoing rows, html code, logic to change row colors etc).

Once you get the hang of it, it becomes very easy to maintain, and debug your application, as well as add new features and remove other ones.

Let's Do This, Where Do I Start?
First download the library from . Got it, ok now we need our directory layout
Test ZF Project/

Ok create these files and directories, and now we will go through what they are for.

Folder test ZF Project
This folder is where your files for your project are located. This is NOT the document Root. This could be something like

Folder Application
This is where all of your application files exists, everything from configs to your models/views/controllers, helper scripts, and plugins.

Folder Application/Config and File config.ini,bootstrap.php
This folder holds configuration data for your application, and config.ini is a global config file. bootstrap.php is where your application
is setup and run from, some people make the index.php in the document root their bootstrap, there is nothing wrong with this, I just like it this way.

Folder controllers
This is where all of your controllers are located.

Folder models
This is where all of your models are located.

Folder Plugins
We store plugins, such as extended classes we create, in here.

Folder Views, Sub directory helpers/layouts/scripts
This contains to view information. The folder Helpers stores our custom view helpers,
the folder layouts contains our site layouts if we use zend_Layout, more on this later.

Scripts folder holds the view logic for each controller.

Folder Library
This is the library folder from the Zend Framework archive we downloaded, this is the .... library :)

Folder Public and sub folders
THIS IS YOUR DOCUMENT ROOT. This is the public access of your site, this is the starting point. All of your
files that need to be accessed directly should be organized here.

Scripts/styles/media/images etc, these are folders for holding javascript,css, media, and image files used by your application.

Folder Scripts
A lot of people do not include this folder in their layout, or even talk about it. A lot of times your application may need to execute stand-alone
scripts for things like cleanup, or pruning, etc. , this is where you put your scripts at.

Folder Tmp
This folder holds temporary data, things such as cached output. You should generally have a script to prune this directory often if you use it.
I suggest if you use cache, you should have a subdirectory called 'cache' in the Tmp directory, and use it that way, just a lot cleaner.

Step 1-A. public/.htaccess
You need to rewrite requests to your public folder to be handled by out bootstrap, by funneling them to index.php , here is what you
want the contents of your .htaccess file to be, of course you can change it up, this is just a simple layout.

RewriteEngine on 
RewriteRule !(\.(js|ico|gif|jpg|png|css|swf|avi|mpg|mpeg|wmv|pdf|ttf))$ index.php

As you see any request for something that is not a public file, will be directed to index, now this may not be what you want, you may want
to setup a rewrite rule that checks if the file exists, and if it does not, then direct it to index.php

Save this file, now on to index.php

Step 1-B. public/index.php
I like my bootstrap in the application directory, so this file is simply a required call to that file.


Painless, easy to understand. On to the Bootstrap.

Step 2-a. Application/config/bootstrap.php
This file is the heart of your application. It sets up how your app should run, and your includes, and runs the app itself.

Please check the documentation for more information. However in this scope, I will provide comments to the bootstrap for you to follow along

/* Report all errors directly to the screen for simple diagnostics in the dev environment */  
error_reporting(E_ALL | E_STRICT);  
ini_set('display_startup_errors', 1);  
ini_set('display_errors', 1); 
 * Add the Zend Framework library to the include path so that we can access the ZF classes
 * Also , Add the models directory, so we can take advantage of autoloading them in controllers.
set_include_path('../library' . PATH_SEPARATOR . get_include_path() . PATH_SEPARATOR . '../application/models');  
/* Set up autoload so we don't have to explicitely require each Zend Framework class */ 
require_once "Zend/Loader.php"; 

 * Setup the Zend Registry
 * This will take information from config.ini and
 * store it for use in the application
$config = new Zend_Config_Ini('../application/config/config.ini', 'general'); // general corresponds to the section inside the ini file, you will see later
$registry = Zend_Registry::getInstance();
$registry->set('config', $config);

/* Our database info was stored in config.ini, so we will initialize our connection here
* If you do not want to use a database, of course, omit this section. This also stores the connection in the registry.
$db = Zend_Db::factory($config->db->adapter,

/* Grab instance of the front controller */
$frontController = Zend_Controller_Front::getInstance(); 
/* Disable error handler so it doesn't intercept all those errors we enabled above or set
 * to true to show errors during development, FALSE ON LIVE SERVER
/* Setup controller directory */

/* Run the application */ 

Ok save this file, now lets setup our database configuration

Step 2-b. Application/Config/config.ini
Omit this section if you are not using a database.

This is very self explanatory
db.adapter = MYSQLI = localhost
db.config.username = username
db.config.password = password
db.config.dbname = database

The 'general' part is just the section, we could, create a 'live' and 'dev' section for diff databases etc, this also corresponds to the database
section in the bootstrap above.

Step 3-a Understanding and using Controllers
Controllers handle programming logic. For the scope of this tutorial we will keep it simple.

In ZF, the router takes the requested url into account and breaks it down like this
so that this url
would translate this way.

Controller = ArticlesController
Action = viewAction
Variables = $_GET['id'] = 13

Make sense?

When you request a page without specifying a controller, the default controller IndexController is used,
whenever you do not specify an action, the default action indexAction is used.

So enough jazz, lets create our first controller, this will handle the main page for the site.


class IndexController extends Zend_Controller_Action 
	public function indexAction() 
		// Set a variable to be retrieved inside our view script
		$this->view->title = "Hello World!";
	public function helpAction()
		$this->view->title = "Help Page";

As you see it is very simple. Controllers extend Zend_Controller_Action , and the name of the file is NameController.php where Name= the controller name.
The class is also NameController where name again equals the controllers name.

Actions, are named the same way, nameAction() , where name= the actions' name.

We can set variables to be retrieved by our view script by using $this->view->varname = 'value'; where varname = the variables name,
and value = the value you want to set.

Now to view these actions you can visit
// Access IndexController with indexAction()  

// Access IndexController with helpAction()

But wait, it errors out, (or doesnt if throwexceptions is false in bootstrap), that is because we need view scripts to correspond
to these actions.

Step 3-b Understanding and using Views

View scripts handle logic pertaining to presentation.

Our view directory is application/views/scripts

For each Controller we will create a new sub-directory inside this folder.

So say we have a controller named 'AuthController' we will need to create a directory named 'auth'.

So for our example, we need a view directory for our 'IndexController' , so we will create the directory

Now inside this directory, we will need a view script for each action in our controller. These files are saved
in the convention action-name.phtml make sure to note the .phtml, if you save it as .php it will
not find it.

So for our actions indexAction() and helpAction() we will need 2 files

Here we will show what our files may look like

<h1><?php echo $this->title; ?></h1>

Welcome to the home page

(c)2008 Test Project

<h1><?php echo $this->title; ?></h1>

Welcome to the help page

(c)2008 Test Project

As you see, echoing $this->varname will pull the variables we set in our controller. Nifty huh?

Of course, Don't Repeat Yourself (DRY) , we see both files do the same thing in the header and footer, so why are we repeating ourselves. Couldn't we
just create a common header and footer file, then include these, making it easier to change layouts?

Of Course. Let us take a look.

<h1><?php echo $this->title; ?></h1>

(c)2008 Test Project

Now we just modify our view scripts like so (im only going to show one, you get the picture though)

<?php echo $this->render('header.phtml'); ?>

Welcome to the home page

<?php echo $this->render('footer.phtml'); ?>

AWesome huh?

Now let's view our site. Visit the following.

Yayyyy it works!

We all say the same thing when starting. To tell you the truth, PHP is a templating engine itself. There are clean ways
to cycle through arrays, and do conditionals and such. take the following.

Say our controller sets this variable $this->view->employees = array('Joey','Kyle','Stuart');

Well we can do this in our view script
<?php foreach($employees as $employee) : ?> 

Employee: <?php echo $employee; ?> <br/>

<?php endforeach; ?>

Or with a conditional

<?php if($employeeName = 'Joey') : ?>

Hello Master!
<?php else : ?>

Hello <?php echo $employeeName; ?>

<?php endif; ?>

Now, I am not against template engines. If you read the post on the php section about smarty you will see what I mean. They are great, and work wonders
for sloppy code, or inept designers, and promote good habits, readability, and easy to switch templates. However if you are using an MVC framework like this,
everything is already seperated, and although it is possible to incorporate Smarty into ZF rather easily, I do not see that it is necessary.

Step 3-c Using Models
In our example so far we have setup our application to work. However now we want to use a database, and have set our connection up already above
in our config and bootstrap. Now we must create our models.

Models naming convention is TableName.php , and is stored in application/models/ .

So say we have a table named employees, we would create a file application/models/Employees.php , and here is what it would look like.

class Employees extends Zend_Db_Table_Abstract

		protected $_name = "employees";


Very simple, we create a class named tablename and extend Zend_Db_Table_Abstract. Now we create a variable named $_name = "tablename";, very
very easy to understand. And that is all.

Now lets put it to use in our controller.

public function indexAction(){
  $e = new Employees();
  $employee = $e->fetchRow("name='Joey'");
  $this->view->employeeID = $employee->id;


Also a little easy to understand. Since we set our include path to include our models directory, we can use 'Zend_Loader::loadClass()' to load our model for

us. Then we create an instance of it. We fetch a row where name='joey' , then set a variable in our view called employeeID to equal the id column from the

row retrieved.

Now that it works, have fun, and experiment. Get used to all the things it has to offer. Since this is a large tutorial already, I will create more
to come to show you how to work with databases, and how to spice things up with extending, creating custom helpers, and some more neat advanced aspects of

zend framework.

Keep in mind this is a simple form, and tutorial, check out the documentation and the API from the site below to learn more, and start playing around
with my favorite framework.

Zend Framework:
ZF Manual:
ZF Tuts:
More Tuts(unofficial):
My Blog:

Is This A Good Question/Topic? 3
  • +

Replies To: Introduction to Zend Framework

#2 mimiten   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-October 08

Posted 26 October 2008 - 04:29 AM

thanks for the tut :)
Was This Post Helpful? 0
  • +
  • -

#3 truong12a62009   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-December 08

Posted 20 December 2008 - 05:56 PM

good tutorials. Thanks! You can introduction to the Zend Framework Part 2?
Was This Post Helpful? 0
  • +
  • -

#4 supersssweety   User is offline

  • D.I.C Regular
  • member icon

Reputation: 22
  • View blog
  • Posts: 373
  • Joined: 16-March 07

Posted 02 August 2009 - 02:11 PM

Great turtorial, I have the index page working and talking to my database...lovely! On the help page gives me an error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (help)' any thoughts? I set everything up like you said.

I would love to see more turtorials w/ Zend
Was This Post Helpful? 0
  • +
  • -

#5 joeyadms   User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 178
  • Joined: 04-May 08

Posted 05 August 2009 - 08:22 AM

View Postsupersssweety, on 2 Aug, 2009 - 01:11 PM, said:

Great turtorial, I have the index page working and talking to my database...lovely! On the help page gives me an error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (help)' any thoughts? I set everything up like you said.

I would love to see more turtorials w/ Zend

It looks like I made an error with the url in the tutorial :( , I can't be 100%, but my guess is that the url you are trying is similiar to , which would make the framework look for a controller named 'help'. Try using the following url and see if it works for you
Was This Post Helpful? 0
  • +
  • -

#6 gregwhitworth   User is offline

  • Tired.
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,613
  • Joined: 20-January 09

Posted 05 August 2009 - 08:53 AM

I will definitely do this, I have been looking into frameworks and heard that Zend was the best. Thanks for the tut.
Was This Post Helpful? 0
  • +
  • -

#7 Feuer   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 57
  • Joined: 15-June 11

Posted 13 May 2012 - 02:28 AM

I made an example-project following this tutorial, and it works fine on XAMPP, but uploading it to my public server's root dir (the one above public_html) gives me this. What is the difference between the servers? What is broken? What information do you need to comment on this?
Was This Post Helpful? 0
  • +
  • -

#8 joeyadms   User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 178
  • Joined: 04-May 08

Posted 13 May 2012 - 01:07 PM

View PostFeuer, on 13 May 2012 - 02:28 AM, said:

I made an example-project following this tutorial, and it works fine on XAMPP, but uploading it to my public server's root dir (the one above public_html) gives me this. What is the difference between the servers? What is broken? What information do you need to comment on this?

It appears that it cannot load the 'index' action from within the default controller. Make sure inside your default controller class (IndexController.php perhaps) that you have implemented an 'indexAction' method.

ZF has also evolved many times since this tutorial , 1.11x and 2.x versions are out so it may be of better help to look up tutorials for these versions on akrabat's website,

Also, since you say uploading to your host doesn't work, it may be a filename case issue if your host is linux. Make sure your directory structure and file names follow the standard conventions. It may be as simple as renaming indexController.php to IndexController.php etc.
Was This Post Helpful? 0
  • +
  • -

#9 Feuer   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 57
  • Joined: 15-June 11

Posted 15 May 2012 - 01:26 AM

Here is an image of the app-folder (renamed to devprong_app) and the default controller. Curiously indexAction seems to not exists, but helpAction does. But yes, you are correct: I probably should play with newer Zend tutorial.

E: I removed Zend from the host and hereby declare my links dead

This post has been edited by Feuer: 15 May 2012 - 01:36 AM

Was This Post Helpful? 0
  • +
  • -

#10 rajendrapatil   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-June 12

Posted 23 June 2012 - 06:00 AM

Hey thank you so much!! It helped me to understand the zend structure much better. I would also appreciate if you could add some more examples related to connecting forms and fetching data from them to mysql database. As in my case i have simply created a lgin form with attribute as username, email-id, password, where i kept my first attribute as id which is auto-increment. Not able to understand exactly how to proceed further. What are the step that i need to follow exactly to get it connected to the database. I have already created database in mysql with these attribute. Hoping for som good suggestion. Thank you...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1