introduction to zend framework
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.TRANSCRIPT
![Page 1: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/1.jpg)
An Introduction to Zend Framework
Jamie Hurst
![Page 2: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/2.jpg)
Who is this guy?• PHP slave at
LINGsCARS.com
•Occasional freelancer
• Blogger
•Geekest drink frequenter
•@JamieFDHurst
![Page 3: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/3.jpg)
Getting the Framework
http://framework.zend.com/* you will need to register!
![Page 4: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/4.jpg)
InstallationCopy your
downloaded directory
somewhere handy...
Add this include path into your php.ini file...
![Page 5: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/6.jpg)
New Project
zf.sh create project <myproject>
ControllersModelsViews
![Page 7: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/7.jpg)
New ProjectIf your web server is setup correctly,
you should see this!
Applicationis accessed
from thepublic folder
![Page 8: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/8.jpg)
A Quick Zend Overview
•Models (db-tables, object models)
•Views (helpers, scripts, layouts)
•Controllers (actions, helpers)
•Forms
•Modules
![Page 9: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/9.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/10.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/11.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/12.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/13.jpg)
ControllersControllers have many actions, and an init() method
Action methods end in “Action”
![Page 14: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/14.jpg)
ControllersEach action has a corresponding view script
Variables and helpers can be accessed
![Page 15: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/15.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/16.jpg)
Displaying PostsThe view script accesses variables
passed by the controller
Note theclever
url helper!
![Page 17: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/17.jpg)
The Result...Ta-da! Posts displaying on your index page!
But there’s something missing...
![Page 18: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/18.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/19.jpg)
Categories and Comments A custom
table method is
used...Which
uses the where
functionality
![Page 20: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/20.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/21.jpg)
Categories and Comments
The result shows the categories and count of comments!
![Page 22: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/22.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/23.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/24.jpg)
LayoutsAnd the result of the layout wrapping:
![Page 25: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/25.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/26.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/27.jpg)
Individual PostsNow to build a quick view script for the action...
![Page 28: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/28.jpg)
Individual Posts
Now let’s make a quick comment form...
![Page 29: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/29.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/30.jpg)
Creating the Form
zf.sh create form <name>Simply add
the elements in the init method
![Page 31: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/31.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/32.jpg)
Including the FormSimply echoing the form variable displays
all the elements and any errors
![Page 33: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/33.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/34.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/35.jpg)
Creating an RSS FeedThe view script for this context is
views/scripts/index/index.xml.phtml
![Page 36: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/36.jpg)
RSS FeedAnd there is our finished feed!
![Page 37: Introduction to Zend Framework](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/37.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022012310/558fcfde1a28ab3f1b8b461f/html5/thumbnails/38.jpg)
Thank you!Feel free to send any Zend-related questions
to [email protected],or tweet me @JamieFDHurst.