Getting Started with Zend Framework


Published on

Introduction to Zend Framework and developing your first application with Zend Framework's MVC.

Published in: Technology, Education
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Getting Started with Zend Framework

  1. 1. Getting Started with Zend Framework Matthew Weier O'Phinney Software Architect Zend Framework
  2. 2. <ul><li>Just Another PHP Hacker </li></ul>Who's this guy?
  3. 3. Matthew Weier O'Phinney <ul><li>Contributor to Zend Framework since pre-0.1.0; used ZF internally at Zend. </li></ul><ul><li>Component lead on MVC since August 2006. </li></ul><ul><li>Full-time Zend Framework developer since December 2007. </li></ul><ul><li>Software Architect since April 2008. </li></ul>
  4. 4. What we'll cover <ul><li>Bird's-eye view of Zend Framework </li></ul><ul><li>How to start integrating Zend Framework in your applications </li></ul><ul><li>MVC overview </li></ul><ul><li>Quick Start to developing applications using Zend Framework's MVC </li></ul>
  5. 5. What is Zend Framework? It's just another PHP framework
  6. 6. No, what is Zend Framework? It's a glue library.
  7. 7. No, really, what is Zend Framework? <ul><li>PHP 5 library for web development productivity </li></ul><ul><li>Open source </li></ul><ul><ul><li>New BSD license is business-friendly </li></ul></ul><ul><ul><li>Free for development and distribution </li></ul></ul><ul><ul><li>CLA process assures that the code is free of legal issues </li></ul></ul><ul><li>Class library – fully OOP </li></ul><ul><li>Documentation – in many languages </li></ul><ul><li>Quality & testing – fully unit tested </li></ul><ul><ul><li>>80% code coverage required; >90% encouraged </li></ul></ul>
  8. 8. Zend Framework Philosophy <ul><li>Simplicity and Extensibility </li></ul><ul><ul><li>Easy solutions for the 80% most commonly-used functionality for web applications </li></ul></ul><ul><ul><li>Extensibility enables easy customization, to solve the remaining 20% </li></ul></ul><ul><ul><li>No complex XML configuration files </li></ul></ul><ul><li>Good object-oriented and agile practices </li></ul><ul><ul><li>Use-at-will architecture, but also: </li></ul></ul><ul><ul><li>Full stack framework </li></ul></ul><ul><ul><li>Designed for extensibility </li></ul></ul><ul><ul><li>Frequent testing </li></ul></ul><ul><ul><li>Frequent interaction with user community </li></ul></ul>
  9. 9. Zend Framework quality process <ul><li>Say what you’re going to do </li></ul><ul><ul><li>Proposal process </li></ul></ul><ul><li>Do it </li></ul><ul><ul><li>Write object oriented components </li></ul></ul><ul><ul><li>Unit test your component </li></ul></ul><ul><ul><ul><li>We encourage test-driven development (TDD)‏ </li></ul></ul></ul><ul><ul><li>Document how it works </li></ul></ul><ul><li>Verify it matches what you said </li></ul><ul><ul><li>Open-source development and community review </li></ul></ul><ul><ul><li>Frequent and thorough testing with PHPUnit </li></ul></ul><ul><ul><li>Code coverage reports with PHPUnit </li></ul></ul><ul><ul><li>Review by internal Zend team for compliance </li></ul></ul>
  10. 10. What's in Zend Framework?
  11. 11. How to get started
  12. 12. Two approaches <ul><li>Start using individual components in your existing applications </li></ul><ul><ul><li>Gradual migration path </li></ul></ul><ul><ul><li>Add new functionality to your application as you need it </li></ul></ul><ul><ul><li>Replace existing functionality with well-tested components </li></ul></ul><ul><li>Start using the Zend Framework MVC layer </li></ul><ul><ul><li>Develop new applications </li></ul></ul><ul><ul><li>Have ZF intercept new functionality in your site </li></ul></ul><ul><ul><li>Create well-tested and testable applications in Zend Framework </li></ul></ul>
  13. 13. MVC Overview
  14. 14. Model-View-Controller <ul><li>Model : Business logic and abstraction </li></ul><ul><li>View : Presentation layer </li></ul><ul><li>Controller : Decide which Models and Views to utilize, based on request </li></ul>
  15. 15. Front Controller <ul><li>Intercept all requests </li></ul><ul><li>Map requests to Action Controllers </li></ul><ul><li>Dispatch requested Action Controller and return response </li></ul>
  16. 16. Benefits of MVC <ul><li>Separate your code into discrete realms of responsibility </li></ul><ul><ul><li>Business logic </li></ul></ul><ul><ul><li>Presentation logic </li></ul></ul><ul><ul><li>Route decisioning </li></ul></ul><ul><li>Predictable location of code on the server </li></ul><ul><li>Typically utilizes OOP => easier to test and re-use </li></ul><ul><ul><li>Easier to maintain long term </li></ul></ul>
  17. 17. Quick Start
  18. 18. Setup the Project Structure <ul><li>First things first: create your directory structure </li></ul>
  19. 19. Get Zend Framework <ul><li>From </li></ul>
  20. 20. Get Zend Framework <ul><li>Extract the archive </li></ul><ul><ul><li>tar xzf ZendFramework-1.6.1.tar.gz </li></ul></ul><ul><ul><li>Unzip </li></ul></ul><ul><ul><li>Or use a GUI frontend </li></ul></ul><ul><li>Copy or symlink the library/Zend directory you extracted into your library directory: </li></ul>
  21. 21. Create a Virtual Host <ul><li>Simplest version: </li></ul>
  22. 22. Create your .htaccess file <ul><li>php.ini directives: </li></ul><ul><ul><li>date.timezone: many apps rely on this </li></ul></ul><ul><ul><li>short_open_tags: view scripts are easier to write with this on </li></ul></ul><ul><ul><li>error_reporting: default error reporting level – E_ALL|E_STRICT for development </li></ul></ul><ul><ul><li>display_errors: turn on for development </li></ul></ul>
  23. 23. Create your .htaccess file <ul><li>RewriteRules: any request to a file that does not exist should go to our Zend Framework bootstrap: </li></ul>
  24. 24. Create your bootstrap files <ul><li>index.php: </li></ul><ul><ul><li>Indicate we need bootstrapping for our application </li></ul></ul><ul><ul><li>Load our bootstrap file, and handle any errors </li></ul></ul><ul><ul><li>Dispatch our front controller </li></ul></ul>
  25. 25. index.php
  26. 26. Create your bootstrap files <ul><li>application/bootstrap.php: </li></ul><ul><ul><li>Setup application constants </li></ul></ul><ul><ul><li>Web bootstrap related setup </li></ul></ul><ul><ul><ul><li>include_path </li></ul></ul></ul><ul><ul><ul><li>Setup autoloading </li></ul></ul></ul><ul><ul><li>Front controller setup </li></ul></ul><ul><ul><ul><li>Add a controller directory </li></ul></ul></ul><ul><ul><ul><li>Set some application parameters </li></ul></ul></ul>
  27. 27. application/bootstrap.php
  28. 28. Create a controller <ul><li>All controllers extend Zend_Controller_Action </li></ul><ul><li>Naming conventions </li></ul><ul><ul><li>Controllers end with 'Controller': IndexController, GuestbookController </li></ul></ul><ul><ul><li>Action methods end with 'Action': signAction(), displayAction(), listAction() </li></ul></ul><ul><ul><li>Controllers should be in the application/controllers/ directory, and named after the class, with a “.php” suffix: application/controllers/IndexController.php application/controllers/GuestbookController.php </li></ul></ul><ul><ul><li>All “conventions” are configurable! </li></ul></ul>
  29. 29. IndexController.php
  30. 30. But... there's no code there... <ul><li>By default, a view is rendered; you don't need to do anything to enable this. </li></ul><ul><li>The view rendered is <controllername>/<action>.phtml </li></ul><ul><li>The view script path resolution is configurable! </li></ul>
  31. 31. Create a view script <ul><li>View scripts go in application/views/scripts/ </li></ul><ul><li>View script resolution looks for a view script in a subdirectory named after the controller </li></ul><ul><ul><li>Controller name used is same as it appears on the url: </li></ul></ul><ul><ul><ul><li>“GuestbookController” appears on the URL as “guestbook” </li></ul></ul></ul><ul><ul><ul><li>“GuestBookController” appears on the URL as “guest-book” </li></ul></ul></ul><ul><ul><ul><li>Basically, MixedCased becomes dash-separated </li></ul></ul></ul><ul><li>View script name is the action name as it appears on the url: </li></ul><ul><ul><li>“signAction()” appears on the URL as “sign” </li></ul></ul><ul><ul><li>“myAccountAction()” appears on the URL as “my-account” </li></ul></ul><ul><ul><li>Basically, camelCased becomes dash-separated </li></ul></ul>
  32. 32. index/index.phtml view script
  33. 33. But... that's just HTML! <ul><li>View scripts use PHP as their templating language – PHP is a template language! </li></ul><ul><li>Mix HTML and PHP </li></ul><ul><li>Use alternate control structure annotations for readability </li></ul><ul><li>Use short tags (heavily debated) for brevity </li></ul>
  34. 34. Create an ErrorController <ul><li>Application errors are trapped by the front controller by default </li></ul><ul><li>When exceptions are detected, ErrorController::errorAction() is invoked </li></ul><ul><li>Separate 404 (Not Found) errors from 500 (Application) errors to return proper response codes to the user </li></ul><ul><li>Display errors in development, log them in production </li></ul><ul><li>Alternately, do something else: </li></ul><ul><ul><li>Perform a search based on the originally requested URL </li></ul></ul><ul><ul><li>Display a site map </li></ul></ul>
  35. 35. ErrorController.php
  36. 36. Create a view script for the ErrorController <ul><li>We have a controller and action, so we need a view script </li></ul><ul><li>Appears in error/error.phtml of the view scripts directory </li></ul>
  37. 37. error/error.phtml
  38. 38. But wait, these aren't complete HTML pages! <ul><li>You're right – they are your application views </li></ul><ul><li>This leads into our next topic: layouts </li></ul>
  39. 39. Layouts – Design Patterns <ul><li>Two Step View </li></ul><ul><ul><li>Inject application views into a common view to return </li></ul></ul><ul><ul><li>Usually associated with a Transform View – think XSLT </li></ul></ul><ul><li>Composite View </li></ul><ul><ul><li>Inject rendered views into a common view, OR </li></ul></ul><ul><ul><li>Render additional views from a master view </li></ul></ul><ul><li>In a nutshell: build the site view, or layout, from lots of little building blocks, or application views </li></ul>
  40. 40. Layouts – Example Use Cases <ul><li>We want our application views to appear in this: </li></ul>
  41. 41. Zend_Layout <ul><li>Steps: </li></ul><ul><li>Initialize layouts </li></ul><ul><li>Perform view initialization (set doctype, etc) </li></ul><ul><li>Create a layout script </li></ul>
  42. 42. Zend_Layout: bootstrap changes
  43. 43. Zend_Layout: layout script
  44. 44. Configuring your Application <ul><li>Zend Framework is configuration-less </li></ul><ul><li>But your application may need configuration </li></ul><ul><li>Zend_Config... </li></ul><ul><ul><li>allows you to write configurations in several formats </li></ul></ul><ul><ul><li>provides OOP access to your configuration </li></ul></ul><ul><ul><li>provides configuration inheritance between sections </li></ul></ul><ul><li>Zend_Registry allows you to persist objects – including your configuration – for the duration of the request </li></ul><ul><ul><li>Put stuff in </li></ul></ul><ul><ul><li>Take stuff out </li></ul></ul>
  45. 45. Sample INI configuration skeleton
  46. 46. Configuration: bootstrap.php changes
  47. 47. What about the M? The Model? <ul><li>Patterns : </li></ul><ul><li>Table Data Gateway </li></ul><ul><ul><li>Abstracts SQL access to a single database table and its rows </li></ul></ul><ul><ul><li>Often used with Row Data Gateway, which abstracts access to a single table row </li></ul></ul><ul><li>Table Module </li></ul><ul><ul><li>Abstracts and restricts access to a table </li></ul></ul><ul><ul><li>Defines entry points to the table </li></ul></ul><ul><ul><li>Entry methods contain business logic </li></ul></ul><ul><li>Domain Model </li></ul><ul><ul><li>Abstracts entities and their relationships </li></ul></ul><ul><ul><li>Not necessarily a 1:1 correspondence with tables </li></ul></ul>
  48. 48. Setting up the Database Adapter <ul><li>Zend_Db::factory() will instantiate the requested adapter </li></ul><ul><li>Pass our configuration to it </li></ul><ul><ul><li>Negates the need to modify code going from development to testing to production </li></ul></ul><ul><li>Set the adapter as the default adapter for our Table Data Gateway (Zend_Db_Table) </li></ul>
  49. 49. DB Adapter – Configuration and Bootstrap
  50. 50. Table Data Gateway: Zend_Db_Table <ul><li>Abstracts SQL for interacting with a database table </li></ul><ul><li>Fetch Rowsets or individual Rows </li></ul><ul><li>Override methods to enforce data integrity </li></ul><ul><li>In our application </li></ul><ul><ul><li>Ensure we get a “created” timestamp on each row </li></ul></ul><ul><ul><li>Prevent updates to existing rows </li></ul></ul>
  51. 51. Model_DbTable_Guestbook
  52. 52. Model: Table Module <ul><li>Identify access methods: </li></ul><ul><ul><li>Save entries </li></ul></ul><ul><ul><li>Fetch all entries </li></ul></ul><ul><ul><li>Fetch individual entry </li></ul></ul><ul><li>Attach a data source </li></ul><ul><ul><li>Often a Table Data Gateway </li></ul></ul><ul><ul><li>In our case, Model_DbTable_Guestbook </li></ul></ul>
  53. 53. Table Module – Retrieve Table Data Gateway
  54. 54. Table Module – Access Methods
  55. 55. Using the Model in the Controller <ul><li>Controller needs to retrieve Model </li></ul><ul><li>To start, let's fetch listings </li></ul>
  56. 56. Adding the Model to the Controller
  57. 57. Create a Form <ul><li>Zend_Form: </li></ul><ul><li>Provides input filtering: </li></ul><ul><ul><li>Filter chains to sanitize data prior to validation </li></ul></ul><ul><ul><li>Validation chains to ensure the data provided is valid </li></ul></ul><ul><li>Allows rendering form </li></ul><ul><ul><li>Follows decorator pattern </li></ul></ul><ul><ul><li>Completely configurable </li></ul></ul><ul><ul><li>Output in any format your application needs: HTML, PDF, XML, etc. </li></ul></ul>
  58. 58. Create a form – Identify elements <ul><li>Guestbook form: </li></ul><ul><li>Email address </li></ul><ul><li>Comment </li></ul><ul><li>Captcha to reduce spam entries </li></ul><ul><li>Submit button </li></ul>
  59. 59. Create a form – Guestbook form
  60. 60. Create a form – Guestbook form (cont.)
  61. 61. Using the Form in the Controller <ul><li>Controller needs to fetch form object </li></ul><ul><li>On landing page, display the form </li></ul><ul><li>On POST requests, attempt to validate the form </li></ul><ul><li>On successful submission, redirect </li></ul>
  62. 62. Adding the form to the controller
  63. 63. Demonstration
  64. 64. Summary
  65. 65. What we learned <ul><li>Recommended project structure </li></ul><ul><li>How to get and install Zend Framework </li></ul><ul><li>How to configure your Apache vhost </li></ul><ul><li>How to setup your php.ini settings and RewriteRules </li></ul><ul><li>What your bootstrap files are and what they should contain </li></ul><ul><li>How to create Action Controllers and View Scripts </li></ul><ul><li>How to create and initialize layouts </li></ul><ul><li>How to use configuration and the registry </li></ul><ul><li>How to create a model, including database access </li></ul><ul><li>How to create a form </li></ul>
  66. 66. Where to go from here <ul><li>Zend Framework QuickStart Guide: </li></ul><ul><li>Zend Framework Manual </li></ul><ul><li>Zend Framework Wiki (proposals, tutorials, etc.) </li></ul><ul><li>Zend Developer Zone (tutorials, articles, announcements) </li></ul>
  67. 67. Thank you!