46
Features and Common Patterns Zend Framework Introduction

Zend Framework Introduction

Embed Size (px)

DESCRIPTION

Features and patterns in Zend Framework

Citation preview

Page 1: Zend Framework Introduction

Features and Common Patterns

Zend Framework Introduction

Page 2: Zend Framework Introduction

Examples from a demo blogging application named Postr are used throughout this presentation. You can view, download, or fork the demo web application on GitHub:

http://github.com/bradley-holt/postr

Page 3: Zend Framework Introduction

Automated scaffolding of project and project components

Used in creating the demo application, Postr

Referenced throughout this presentation

Zend_Tool

Page 4: Zend Framework Introduction

Zend_Tool

Create a Projectmkdir postrcd postrzf create project .

Page 5: Zend Framework Introduction

Zend_Tool

Project Structure.zfproject.xmlapplication/ Bootstrap.php configs/ application.ini controllers/ ErrorController.php IndexController.php views/ scripts/ error/ error.phtml index/ index.phtmlpublic/ .htaccess index.phptests/ application/ bootstrap.php library/ bootstrap.php phpunit.xml

Page 8: Zend Framework Introduction

Con!gurationDefault con"guration is in application/configs/application.ini

Allows for con"guration sections; for example:• production• staging• testing• development

Sections can inherit from other sections

See:application/con"gs/application.ini

Page 9: Zend Framework Introduction

Name the ProjectDefault application class name pre"x is Application_.zf change application.class-name-prefix Postr_

Page 10: Zend Framework Introduction

Updated Con!gurationAdded to application/configs/application.ini:[production]appnamespace = "Postr_"

See:application/con"gs/application.ini

Page 12: Zend Framework Introduction

Zend_LayoutImplementation of the Two Step View pattern

Allows for consistent layout across multiple pages

Easier to manage than “includes”

See:Zend_LayoutTwo Step View

Page 13: Zend Framework Introduction

Zend_Layout

Enable Layoutzf enable layout

Page 14: Zend Framework Introduction

Zend_Layout

Layout View Scriptapplication/ layouts/ scripts/ layout.phtml

Added to application/configs/application.ini:[production]resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

See:application/layouts/scripts/layout.phtmlapplication/con"gs/application.ini

Page 15: Zend Framework Introduction

ControllersConnects the Model and the View

Contains one or more actions

URL based routing typically decides what controller and action to execute::controller/:action

Custom routing options available

Page 16: Zend Framework Introduction

View ScriptsPHP templates

No domain logic please!

Default suffix of .phtml

One view script per controller action (by default)

Page 17: Zend Framework Introduction

Create a Controllerzf create controller Entry

Page 19: Zend Framework Introduction

Create Additional Controller Actionszf create action new Entryzf create action get Entryzf create action edit Entryzf create action post Entryzf create action put Entryzf create action delete Entry

Page 20: Zend Framework Introduction

Entry ActionsMethods added to application/controllers/EntryController.php:newAction()getAction()editAction()postAction()putAction()deleteAction()

See:application/controllers/EntryController.php

Page 21: Zend Framework Introduction

Entry ActionsView scripts created:application/ views/ scripts/ entry/ delete.phtml edit.phtml get.phtml new.phtml post.phtml put.phtml

See:application/views/scripts/entry/

Page 22: Zend Framework Introduction

Zend_TestFunctional (end-to-end) testing of controllers

Simulates HTTP requests to the application

No web server required

Also provides a DB testing facility

See:Zend_TestFunctional Testtests/application/controllers/EntryControllerTest.php

Page 23: Zend Framework Introduction

ModelsModels are speci"c to your domainNo such thing as one-size-"ts all modelsNo Zend_ModelHowever, some useful patterns have emerged

Page 24: Zend Framework Introduction

Create a Modelzf create model Entry

Page 27: Zend Framework Introduction

Zend_Form

Create a Formzf create form Entry

Page 29: Zend Framework Introduction

Zend_Db_TableObject-oriented database interface

Implements the Table Data Gateway and Row Data Gateway patterns

See:Table Data GatewayRow Data Gateway

Page 30: Zend Framework Introduction

Con!gure a DB Adapterzf configure dbadapter "adapter=Pdo_Sqlite&dbname=../data/db/production.db"zf configure dbadapter "adapter=Pdo_Sqlite&dbname=../data/db/staging.db" -s stagingzf configure dbadapter "adapter=Pdo_Sqlite&dbname=../data/db/testing.db" -s testingzf configure dbadapter "adapter=Pdo_Sqlite&dbname=../data/db/development.db" -s development

Page 31: Zend Framework Introduction

Updated Con!gurationAdded to application/configs/application.ini:[production]resources.db.adapter = "Pdo_Sqlite"resources.db.params.dbname = APPLICATION_PATH "/../data/db/production.db"

[staging : production]resources.db.adapter = "Pdo_Sqlite"resources.db.params.dbname = APPLICATION_PATH "/../data/db/staging.db"

[testing : production]resources.db.adapter = "Pdo_Sqlite"resources.db.params.dbname = APPLICATION_PATH "/../data/db/testing.db"

[development : production]resources.db.adapter = "Pdo_Sqlite"resources.db.params.dbname = APPLICATION_PATH "/../data/db/development.db"

See:application/con"gs/application.ini

Page 33: Zend Framework Introduction

Create DB Tables from the Databasezf create dbtable.from-database

Page 35: Zend Framework Introduction

Data MapperKeeps your domain logic isolated from your database implementationDomain objects should not directly use data mappers

See:Data Mapper

Page 36: Zend Framework Introduction

Create a Data Mapperzf create model EntryMapper

Page 38: Zend Framework Introduction

Zend_PaginatorPagination for database or any arbitrary data

Several adapters available:• Array

• DbSelect

• DbTableSelect

• Iterator

• Null

• Write your own in order to paginate domain objects

See:Zend_Paginator

Page 39: Zend Framework Introduction

Zend_Paginator

Create a Paginator Adapterzf create model EntryPaginatorAdapter

Page 40: Zend Framework Introduction

Zend_Paginator

Entry Paginator Adapterapplication/ models/ EntryPaginatorAdapter.php

See:application/models/EntryPaginatorAdapter.php

Page 41: Zend Framework Introduction

Zend_DateManipulate dates and times

Useful for date and time calculations

Allows for input from and output to various formats

Used as a domain object in the Postr demo application:• Entry Updated• Entry Published

See:Zend_Dateapplication/models/Entry.php

Page 42: Zend Framework Introduction

Zend_MarkupRenders BBcode or Textile markup into HTML or other formats

Extensible so may see other markup languages in the future

Used in the Postr demo application:• Entry Content and Entry Summary are stored as Textile

markup• Entry Content and Entry Summary can optionally be

retrieved as HTML

See:Zend_MarkupBBCodeTextileapplication/models/Entry.php

Page 44: Zend Framework Introduction

Controller PluginsAllows developers to hook into various events during the controller process:

• routeStartup()

• dispatchLoopStartup()

• preDispatch()

• postDispatch()

• dispatchLoopShutdown()

• routeShutdown()

See:Controller Pluginsapplication/plugins/RouteContext.php