Upload
bradley-holt
View
13.567
Download
0
Tags:
Embed Size (px)
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
Front ControllerAll HTTP requests for the application go through one script.Apache’s rewrite module (or equivalent) makes this happen.
See:Front Controller patternpublic/index.phppublic/.htaccess
Zend_ApplicationBootstraps the application
Provides reusable resources
Sets up PHP environment
See:Zend_Applicationapplication/Bootstrap.php
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
Model-View-Controller (MVC)Composite of several design patternsIsolates domain logic from input and presentationModel: domain logicView: presentation layerController: interprets input and passes it to the Model; provides Model data to the View
See:Model-view-controllerapplication/models/application/views/application/controllers/
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
Entry Controller and View Scriptapplication/ controllers/ EntryController.php views/ scripts/ entry/ index.phtmltests/ application/ controllers/ EntryControllerTest.php
See:application/controllers/EntryController.phpapplication/views/scripts/entry/index.phtmltests/application/controllers/EntryControllerTest.php
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
Entry Modelapplication/ models/ Entry.php
See:application/models/Entry.php
Zend_FormInput "ltering
Input validation
Form and element rendering
Huge time saver
See:Zend_FormZend_FilterZend_Validate
Zend_Form
Create a Formzf create form Entry
Zend_Form
Entry Formapplication/ forms/ Entry.php
See:application/forms/Entry.php
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
Load DB SchemaProject-speci"c and not built-in to Zend Framework:mkdir -p data/db
php scripts/load.sqlite.php
See:scripts/load.sqlite.phpscripts/schema.sqlite.sql
Create DB Tables from the Databasezf create dbtable.from-database
Entry and Entry Tag DB Tablesapplication/ models/ DbTable/ Entry.php EntryTag.php
See:application/models/DbTable/Entry.phpapplication/models/DbTable/EntryTag.php
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
Entry Mapperapplication/ models/ EntryMapper.php
See:application/models/EntryMapper.php
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
Zend_NavigationCreate menus, breadcrumbs, links, and sitemaps
Used to create the menu navigation in the Postr demo application
See:Zend_Navigationapplication/Bootstrap.phpapplication/layouts/scripts/header.phtml
Controller PluginsAllows developers to hook into various events during the controller process:
• routeStartup()
• dispatchLoopStartup()
• preDispatch()
• postDispatch()
• dispatchLoopShutdown()
• routeShutdown()
See:Controller Pluginsapplication/plugins/RouteContext.php
ReferencesBradley Holt’s demo application: Postr
Zend Framework Quick Start
Matthew Weier O’Phinney’s demo application: Pastebin
Zend Framework Programmer’s Reference Guide
CreditsAuthorBradley Holt
Layout & DesignJason Pelletier
This presentation licensed under Creative Commons—Attribution 3.0 United States License.