Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,689 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,321 people online right now. Registration is fast and FREE... Join Now!




Introduction to Zend Framework

 
Reply to this topicStart new topic

> Introduction to Zend Framework, Creating your first Application with ZF, start to finish

joeyadms
Group Icon



post 9 Jun, 2008 - 11:53 AM
Post #1


Introduction to the Zend Framework Part 1

Intro
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 http://www.zendframework.com . Got it, ok now we need our directory layout
CODE

Test ZF Project/
               /application
                       /config
                           config.ini
                           bootstrap.php
                       /controllers
                       /models
                       /plugins
                       /views
                           /helpers
                           /layouts
                           /scripts
               /library
               /public
                       /scripts
                       /styles
                       /media
                       /images
                       .htaccess
                       index.php
               /scripts
               /tmp



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
/home/www/TestZFProject

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 smile.gif

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.

.htaccess
CODE

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.

index.php
CODE

require('../application/config/bootstrap.php');


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

application/config/bootstrap.php
CODE

<?php

/* 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";
Zend_Loader::registerAutoload();

/*
* Setup the Zend Registry
* This will take information from config.ini and
* store it for use in the application
*/
Zend_Loader::loadClass('Zend_Config_Ini');
$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,
$config->db->config->toArray());
Zend_Db_Table_Abstract::setDefaultAdapter($db);
$registry->set('db',$db);


/* 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
*/
$frontController->throwExceptions(true);
/* Setup controller directory */
$frontController->setControllerDirectory('../application/controllers');

/* Run the application */
$frontController->dispatch();


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
application/config/config.ini
CODE

[general]
db.adapter = MYSQLI
db.config.host = 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
http://www.site.com/controller/action/variable/value
so that this url
http://www.site.com/articles/view/id/13
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.


application/controllers/IndexController.php
CODE

<?php

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
CODE

// Access IndexController with indexAction()
http://www.site.com/  

// Access IndexController with helpAction()
http://www.site.com/help/


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
application/views/scripts/index

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
CODE

application/views/scripts/index/index.phtml
and
application/views/scripts/index/help.phtml


Here we will show what our files may look like

index.phtml
CODE

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

Welcome to the home page

(c)2008 Test Project


help.phtml
CODE

<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?

WAIT! DRY DRY DRY
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.

application/views/scripts/header.phtml
CODE

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


application/views/scripts/footer.phtml
CODE

(c)2008 Test Project


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

application/views/scripts/index/index.phtml
CODE

<?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.
CODE

http://www.site.com/

http://www.site.com/help/


Yayyyy it works!




WAIT, WHY AM I MIXING PHP AND HTML, ISNT THAT BAD?
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
CODE

<?php foreach($employees as $employee) : ?>

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

<?php endforeach; ?>


Or with a conditional

CODE

<?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.

application/models/Employees.php
CODE

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.

application/controllers/IndexController.php
CODE

public function indexAction(){
  Zend_Loader::loadClass('Employees');
  $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.

Conclusion
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: http://www.zendframework.com
ZF Manual: http://zendframework.com/manual/en/
ZF API: http://zendframework.com/apidoc/core/
ZF Tuts: http://zendframework.com/docs/multimedia
More Tuts(unofficial): http://www.zftutorials.com/
My Blog: http://www.joeyadams.net
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

mimiten
*



post 26 Oct, 2008 - 03:29 AM
Post #2
thanks for the tut smile.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/23/08 07:37AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month