Post on 18-Jul-2015
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.
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
Dec 8, 2008 | Getting Started With Zend Framework5 |
What is Zend Framework?
It's just another PHP framework
Dec 8, 2008 | Getting Started With Zend Framework6 |
No, what is Zend Framework?
It's a glue library.
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
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
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
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
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
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
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
Dec 8, 2008 | Getting Started With Zend Framework18
|
Setup the Project Structure
• First things first: create your directory structure
Dec 8, 2008 | Getting Started With Zend Framework19
|
Get Zend Framework
• From http://framework.zend.com/download/latest
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:
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
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:
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
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
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!
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!
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
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
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
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
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
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
Dec 8, 2008 | Getting Started With Zend Framework40
|
Layouts – Example Use Cases
• We want our application views to appear in this:
Dec 8, 2008 | Getting Started With Zend Framework41
|
Zend_Layout
Steps:
• Initialize layouts
• Perform view initialization (set doctype, etc)
• Create a layout script
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
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
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)
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
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
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
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.
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
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
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
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/