Top Banner
Getting Started with Zend Framework Matthew Weier O'Phinney Software Architect Zend Framework
67
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Getting Started with Zend Framework

Getting Startedwith Zend FrameworkMatthew Weier O'Phinney

Software Architect

Zend Framework

Page 2: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework2 |

Just Another PHP Hacker

Who's this guy?

Page 3: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework3 |

Matthew Weier O'Phinney

•Contributor to Zend Framework since pre-0.1.0; used ZF internally at Zend.

•Component lead on MVC since August 2006.

•Full-time Zend Framework developer since December 2007.

•Software Architect since April 2008.

Page 4: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework4 |

What we'll cover

• Bird's-eye view of Zend Framework

• How to start integrating Zend Framework in your applications

• MVC overview

• Quick Start to developing applications using Zend Framework's MVC

Page 5: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework5 |

What is Zend Framework?

It's just another PHP framework

Page 6: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework6 |

No, what is Zend Framework?

It's a glue library.

Page 7: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework7 |

No, really, what is Zend Framework?

• PHP 5 library for web development productivity

• Open source New BSD license is business-friendly

Free for development and distribution

CLA process assures that the code is free of legal issues

• Class library – fully OOP

• Documentation – in many languages

• Quality & testing – fully unit tested >80% code coverage required; >90% encouraged

Page 8: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework8 |

Zend Framework Philosophy

• Simplicity and Extensibility Easy solutions for the 80% most commonly-used functionality for web

applications

Extensibility enables easy customization, to solve the remaining 20%

No complex XML configuration files

• Good object-oriented and agile practices Use-at-will architecture, but also:

Full stack framework

Designed for extensibility

Frequent testing

Frequent interaction with user community

Page 9: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework9 |

Zend Framework quality process

1.Say what you’re going to do Proposal process

2.Do it Write object oriented components

Unit test your component• We encourage test-driven development (TDD)

Document how it works

3.Verify it matches what you said Open-source development and community review

Frequent and thorough testing with PHPUnit

Code coverage reports with PHPUnit

Review by internal Zend team for compliance

Page 10: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework10

|

What's in Zend Framework?

Page 11: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework11

|

How to get started

Page 12: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework12

|

Two approaches

• Start using individual components in your existing applications• Gradual migration path

• Add new functionality to your application as you need it

• Replace existing functionality with well-tested components

• Start using the Zend Framework MVC layer• Develop new applications

• Have ZF intercept new functionality in your site

• Create well-tested and testable applications in Zend Framework

Page 13: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework13

|

MVC Overview

Page 14: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework14

|

Model-View-Controller

• Model: Business logic and abstraction

• View: Presentation layer

• Controller: Decide which Models and Views to utilize, based on request

Page 15: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework15

|

Front Controller

• Intercept all requests

• Map requests to Action Controllers

• Dispatch requested Action Controller and return response

Page 16: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework16

|

Benefits of MVC

• Separate your code into discrete realms of responsibility• Business logic

• Presentation logic

• Route decisioning

• Predictable location of code on the server

• Typically utilizes OOP => easier to test and re-use• Easier to maintain long term

Page 17: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework17

|

Quick Start

Page 18: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework18

|

Setup the Project Structure

• First things first: create your directory structure

Page 19: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework19

|

Get Zend Framework

• From http://framework.zend.com/download/latest

Page 20: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework20

|

Get Zend Framework

• Extract the archive• tar xzf ZendFramework-1.6.1.tar.gz

• Unzip ZendFramework-1.6.1.zip

• Or use a GUI frontend

• Copy or symlink the library/Zend directory you extracted into your library directory:

Page 21: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework21

|

Create a Virtual Host

• Simplest version:

Page 22: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework22

|

Create your .htaccess file

• php.ini directives:• date.timezone: many apps rely on this

• short_open_tags: view scripts are easier to write with this on

• error_reporting: default error reporting level – E_ALL|E_STRICT for development

• display_errors: turn on for development

Page 23: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework23

|

Create your .htaccess file

• RewriteRules: any request to a file that does not exist should go to our Zend Framework bootstrap:

Page 24: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework24

|

Create your bootstrap files

• index.php: • Indicate we need bootstrapping for our application

• Load our bootstrap file, and handle any errors

• Dispatch our front controller

Page 25: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework25

|

index.php

Page 26: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework26

|

Create your bootstrap files

• application/bootstrap.php:• Setup application constants

• Web bootstrap related setup include_path Setup autoloading

• Front controller setup Add a controller directory Set some application parameters

Page 27: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework27

|

application/bootstrap.php

Page 28: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework28

|

Create a controller

• All controllers extend Zend_Controller_Action

• Naming conventions• Controllers end with 'Controller':

IndexController, GuestbookController

• Action methods end with 'Action':signAction(), displayAction(), listAction()

• Controllers should be in the application/controllers/ directory, and named after the class, with a “.php” suffix:application/controllers/IndexController.phpapplication/controllers/GuestbookController.php

• All “conventions” are configurable!

Page 29: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework29

|

IndexController.php

Page 30: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework30

|

But... there's no code there...

• By default, a view is rendered; you don't need to do anything to enable this.

• The view rendered is <controllername>/<action>.phtml

• The view script path resolution is configurable!

Page 31: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework31

|

Create a view script

• View scripts go in application/views/scripts/

• View script resolution looks for a view script in a subdirectory named after the controller• Controller name used is same as it appears on the url:

“GuestbookController” appears on the URL as “guestbook” “GuestBookController” appears on the URL as “guest-book” Basically, MixedCased becomes dash-separated

• View script name is the action name as it appears on the url:• “signAction()” appears on the URL as “sign”

• “myAccountAction()” appears on the URL as “my-account”

• Basically, camelCased becomes dash-separated

Page 32: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework32

|

index/index.phtml view script

Page 33: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework33

|

But... that's just HTML!

• View scripts use PHP as their templating language – PHP is a template language!

• Mix HTML and PHP

• Use alternate control structure annotations for readability

• Use short tags (heavily debated) for brevity

Page 34: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework34

|

Create an ErrorController

• Application errors are trapped by the front controller by default

• When exceptions are detected, ErrorController::errorAction() is invoked

• Separate 404 (Not Found) errors from 500 (Application) errors to return proper response codes to the user

• Display errors in development, log them in production

• Alternately, do something else:• Perform a search based on the originally requested URL

• Display a site map

Page 35: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework35

|

ErrorController.php

Page 36: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework36

|

Create a view script for the ErrorController

• We have a controller and action, so we need a view script

• Appears in error/error.phtml of the view scripts directory

Page 37: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework37

|

error/error.phtml

Page 38: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework38

|

But wait, these aren't complete HTML pages!

• You're right – they are your application views

• This leads into our next topic: layouts

Page 39: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework39

|

Layouts – Design Patterns

• Two Step View• Inject application views into a common view to return

• Usually associated with a Transform View – think XSLT

• Composite View• Inject rendered views into a common view, OR

• Render additional views from a master view

• In a nutshell: build the site view, or layout, from lots of little building blocks, or application views

Page 40: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework40

|

Layouts – Example Use Cases

• We want our application views to appear in this:

Page 41: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework41

|

Zend_Layout

Steps:

• Initialize layouts

• Perform view initialization (set doctype, etc)

• Create a layout script

Page 42: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework42

|

Zend_Layout: bootstrap changes

Page 43: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework43

|

Zend_Layout: layout script

Page 44: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework44

|

Configuring your Application

• Zend Framework is configuration-less

• But your application may need configuration

• Zend_Config...• allows you to write configurations in several formats

• provides OOP access to your configuration

• provides configuration inheritance between sections

• Zend_Registry allows you to persist objects – including your configuration – for the duration of the request• Put stuff in

• Take stuff out

Page 45: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework45

|

Sample INI configuration skeleton

Page 46: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework46

|

Configuration: bootstrap.php changes

Page 47: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework47

|

What about the M? The Model?

Patterns:

• Table Data Gateway• Abstracts SQL access to a single database table and its rows

• Often used with Row Data Gateway, which abstracts access to a single table row

• Table Module• Abstracts and restricts access to a table

• Defines entry points to the table

• Entry methods contain business logic

• Domain Model• Abstracts entities and their relationships

• Not necessarily a 1:1 correspondence with tables

Page 48: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework48

|

Setting up the Database Adapter

• Zend_Db::factory() will instantiate the requested adapter

• Pass our configuration to it• Negates the need to modify code going from development to testing

to production

• Set the adapter as the default adapter for our Table Data Gateway (Zend_Db_Table)

Page 49: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework49

|

DB Adapter – Configuration and Bootstrap

Page 50: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework50

|

Table Data Gateway: Zend_Db_Table

• Abstracts SQL for interacting with a database table

• Fetch Rowsets or individual Rows

• Override methods to enforce data integrity

• In our application• Ensure we get a “created” timestamp on each row

• Prevent updates to existing rows

Page 51: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework51

|

Model_DbTable_Guestbook

Page 52: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework52

|

Model: Table Module

• Identify access methods:• Save entries

• Fetch all entries

• Fetch individual entry

• Attach a data source• Often a Table Data Gateway

• In our case, Model_DbTable_Guestbook

Page 53: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework53

|

Table Module – Retrieve Table Data Gateway

Page 54: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework54

|

Table Module – Access Methods

Page 55: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework55

|

Using the Model in the Controller

• Controller needs to retrieve Model

• To start, let's fetch listings

Page 56: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework56

|

Adding the Model to the Controller

Page 57: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework57

|

Create a Form

Zend_Form:

• Provides input filtering:• Filter chains to sanitize data prior to validation

• Validation chains to ensure the data provided is valid

• Allows rendering form• Follows decorator pattern

• Completely configurable

• Output in any format your application needs: HTML, PDF, XML, etc.

Page 58: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework58

|

Create a form – Identify elements

Guestbook form:

• Email address

• Comment

• Captcha to reduce spam entries

• Submit button

Page 59: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework59

|

Create a form – Guestbook form

Page 60: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework60

|

Create a form – Guestbook form (cont.)

Page 61: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework61

|

Using the Form in the Controller

• Controller needs to fetch form object

• On landing page, display the form

• On POST requests, attempt to validate the form

• On successful submission, redirect

Page 62: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework62

|

Adding the form to the controller

Page 63: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework63

|

Demonstration

Page 64: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework64

|

Summary

Page 65: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework65

|

What we learned

• Recommended project structure

• How to get and install Zend Framework

• How to configure your Apache vhost

• How to setup your php.ini settings and RewriteRules

• What your bootstrap files are and what they should contain

• How to create Action Controllers and View Scripts

• How to create and initialize layouts

• How to use configuration and the registry

• How to create a model, including database access

• How to create a form

Page 66: Getting Started with Zend Framework

Dec 8, 2008 | Getting Started With Zend Framework66

|

Where to go from here

• Zend Framework QuickStart Guide:http://framework.zend.com/docs/quickstart

• Zend Framework Manualhttp://framework.zend.com/manual/manual

• Zend Framework Wiki (proposals, tutorials, etc.)http://framework.zend.com/wiki

• Zend Developer Zone (tutorials, articles, announcements)http://devzone.zend.com/

Page 67: Getting Started with Zend Framework

Thank you!