38
An Introduction to Zend Framework Jamie Hurst

Introduction to Zend Framework

Embed Size (px)

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

Page 1: Introduction to Zend Framework

An Introduction to Zend Framework

Jamie Hurst

Page 2: Introduction to Zend Framework

Who is this guy?• PHP slave at

LINGsCARS.com

•Occasional freelancer

• Blogger

•Geekest drink frequenter

•@JamieFDHurst

Page 3: Introduction to Zend Framework

Getting the Framework

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

Page 4: Introduction to Zend Framework

InstallationCopy your

downloaded directory

somewhere handy...

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

Page 5: Introduction to Zend Framework

Installation

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

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

Page 6: Introduction to Zend Framework

New Project

zf.sh create project <myproject>

ControllersModelsViews

Page 7: Introduction to Zend Framework

New ProjectIf your web server is setup correctly,

you should see this!

Applicationis accessed

from thepublic folder

Page 8: Introduction to Zend Framework

A Quick Zend Overview

•Models (db-tables, object models)

•Views (helpers, scripts, layouts)

•Controllers (actions, helpers)

•Forms

•Modules

Page 9: Introduction to Zend Framework

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>

Page 10: Introduction to Zend Framework

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

Page 11: Introduction to Zend Framework

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>

Page 12: Introduction to Zend Framework

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

Page 13: Introduction to Zend Framework

ControllersControllers have many actions, and an init() method

Action methods end in “Action”

Page 14: Introduction to Zend Framework

ControllersEach action has a corresponding view script

Variables and helpers can be accessed

Page 15: Introduction to Zend Framework

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

Page 16: Introduction to Zend Framework

Displaying PostsThe view script accesses variables

passed by the controller

Note theclever

url helper!

Page 17: Introduction to Zend Framework

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

But there’s something missing...

Page 18: Introduction to Zend Framework

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

Page 19: Introduction to Zend Framework

Categories and Comments A custom

table method is

used...Which

uses the where

functionality

Page 20: Introduction to Zend Framework

Categories and Comments

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

these objects

together...

Page 21: Introduction to Zend Framework

Categories and Comments

The result shows the categories and count of comments!

Page 22: Introduction to Zend Framework

LayoutsZend provides layouts, which can be

used to wrap the entire view in a consistent script

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

Page 23: Introduction to Zend Framework

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

Page 24: Introduction to Zend Framework

LayoutsAnd the result of the layout wrapping:

Page 25: Introduction to Zend Framework

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

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

.

Page 26: Introduction to Zend Framework

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

Page 27: Introduction to Zend Framework

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

Page 28: Introduction to Zend Framework

Individual Posts

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

Page 29: Introduction to Zend Framework

Zend_Form

•Full extensible support for all HTML elements

•Easy transition to ajax forms using jQuery or other

•Validation

•Decorators

•Customisation

Page 30: Introduction to Zend Framework

Creating the Form

zf.sh create form <name>Simply add

the elements in the init method

Page 31: Introduction to Zend Framework

Including the FormAnd the form to the controller and

handle when it is submitted

Validation is handled very easily in one line

of code

Page 32: Introduction to Zend Framework

Including the FormSimply echoing the form variable displays

all the elements and any errors

Page 33: Introduction to Zend Framework

Completed FormThe labels and elements are all

rendered correctly using their default decorators Labels

and elements wrapped in <dt>s

and <dd>s

Page 34: Introduction to Zend Framework

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

Page 35: Introduction to Zend Framework

Creating an RSS FeedThe view script for this context is

views/scripts/index/index.xml.phtml

Page 36: Introduction to Zend Framework

RSS FeedAnd there is our finished feed!

Page 37: Introduction to Zend Framework

Quick Summary

•Setting up your project - Zend_Tool

•Database configuration & models

•Controllers & actions

•View scripts

•Forms

•MVC context-switching

Page 38: Introduction to Zend Framework

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

to [email protected],or tweet me @JamieFDHurst.