Introduction to Zend Framework

Preview:

DESCRIPTION

A quick introduction to same aspects of the Zend Framework, including installation and setup using Zend_Tool, ZF's MVC architecture, database models, controllers, view scripts, layouts and forms. Delivered at the April meeting of phpNE.

Citation preview

An Introduction to Zend Framework

Jamie Hurst

Who is this guy?• PHP slave at

LINGsCARS.com

•Occasional freelancer

• Blogger

•Geekest drink frequenter

•@JamieFDHurst

Getting the Framework

http://framework.zend.com/* you will need to register!

InstallationCopy your

downloaded directory

somewhere handy...

Add this include path into your php.ini file...

Installation

Make sure your PATH variable references your bin directory...

This allows you to make use of the Zend_Tool command line interface

New Project

zf.sh create project <myproject>

ControllersModelsViews

New ProjectIf your web server is setup correctly,

you should see this!

Applicationis accessed

from thepublic folder

A Quick Zend Overview

•Models (db-tables, object models)

•Views (helpers, scripts, layouts)

•Controllers (actions, helpers)

•Forms

•Modules

Some Basics

•All classes named for their folder (e.g. Application_Model_Post in application/models/post.php)

•Every Zend module is autoloaded when needed

•Default controller/action is Index

•URL format: /<controller>/<action>

Databasepost

•id•category_id•title•content•time

category•id•name

comment

•id•post_id•author•content•time

1 * 1 *

Edit your application/configs/application.ini file...

Tables and RowsCreate a table and

row modelfor each table

Table classes fetch and find rows, and perform

searches...

Row objects handle accessing, updating and

deleting the data

zf.sh create db-table <name> <table-name>zf.sh create model <name>

Database Models

Table classes should specify the $_rowClass

property

Row classes should extend

Zend_Db_Table_Row_Abstract

Table classes should extend

Zend_Db_Table_Abstract

ControllersControllers have many actions, and an init() method

Action methods end in “Action”

ControllersEach action has a corresponding view script

Variables and helpers can be accessed

Displaying PostsAccess the posts table and row models

and pass the results to the view

The fetchall()method allowsyou to query“where”s and“order by”s

Displaying PostsThe view script accesses variables

passed by the controller

Note theclever

url helper!

The Result...Ta-da! Posts displaying on your index page!

But there’s something missing...

What about Categories and

Comments?The post model needs to be modified to get these properties using the other model objects

Here is the getCategory method in thePost model, making use of the find method

Categories and Comments A custom

table method is

used...Which

uses the where

functionality

Categories and Comments

And now, with some simple alterations to the view script...The beauty of chaining

these objects

together...

Categories and Comments

The result shows the categories and count of comments!

LayoutsZend provides layouts, which can be

used to wrap the entire view in a consistent script

Edit your application.ini file to enable them...

LayoutsBuild your layout script with space for

each view script’s content, and put it in the application/layouts/scripts/ folder

This is where the

view script is inserted

LayoutsAnd the result of the layout wrapping:

ActionsAn action for viewing individual posts would be helpful...

zf.sh create action <action> <controller>A blank action is created..

.

Individual PostsWe’ll need a parameter to know which post to display...

This is also a chance to use some conditional statements in the view

script...

We passed this

parameter in the

earlier url helper

Individual PostsNow to build a quick view script for the action...

Individual Posts

Now let’s make a quick comment form...

Zend_Form

•Full extensible support for all HTML elements

•Easy transition to ajax forms using jQuery or other

•Validation

•Decorators

•Customisation

Creating the Form

zf.sh create form <name>Simply add

the elements in the init method

Including the FormAnd the form to the controller and

handle when it is submitted

Validation is handled very easily in one line

of code

Including the FormSimply echoing the form variable displays

all the elements and any errors

Completed FormThe labels and elements are all

rendered correctly using their default decorators Labels

and elements wrapped in <dt>s

and <dd>s

Creating an RSS Feed Now the

Index action will support XML output too, with no

extra changes to

the action at all!

To access different context types, use the format parameter in your GET

query string, e.g. /index/index/format/xml

Creating an RSS FeedThe view script for this context is

views/scripts/index/index.xml.phtml

RSS FeedAnd there is our finished feed!

Quick Summary

•Setting up your project - Zend_Tool

•Database configuration & models

•Controllers & actions

•View scripts

•Forms

•MVC context-switching

Thank you!Feel free to send any Zend-related questions

to jamie@jamiehurst.co.uk,or tweet me @JamieFDHurst.

Recommended