Zend Framework Introduction

Preview:

DESCRIPTION

Features and patterns in Zend Framework

Citation preview

Features and Common Patterns

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

Automated scaffolding of project and project components

Used in creating the demo application, Postr

Referenced throughout this presentation

Zend_Tool

Zend_Tool

Create a Projectmkdir postrcd postrzf create project .

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

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

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

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

See:application/con"gs/application.ini

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

Zend_Layout

Enable Layoutzf enable layout

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

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

View ScriptsPHP templates

No domain logic please!

Default suffix of .phtml

One view script per controller action (by default)

Create a Controllerzf create controller Entry

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

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

See:application/controllers/EntryController.php

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/

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

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

Create a Modelzf create model Entry

Zend_Form

Create a Formzf create form Entry

Zend_Db_TableObject-oriented database interface

Implements the Table Data Gateway and Row Data Gateway patterns

See:Table Data GatewayRow Data Gateway

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

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

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

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

See:Data Mapper

Create a Data Mapperzf create model EntryMapper

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

Zend_Paginator

Create a Paginator Adapterzf create model EntryPaginatorAdapter

Zend_Paginator

Entry Paginator Adapterapplication/ models/ EntryPaginatorAdapter.php

See:application/models/EntryPaginatorAdapter.php

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

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

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

• routeStartup()

• dispatchLoopStartup()

• preDispatch()

• postDispatch()

• dispatchLoopShutdown()

• routeShutdown()

See:Controller Pluginsapplication/plugins/RouteContext.php

Recommended