Introduction to Zend Framework


Published on

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.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction to Zend Framework

  1. 1. An Introduction to Zend Framework <ul><li>Jamie Hurst </li></ul>
  2. 2. Who is this guy? <ul><li>PHP slave at </li></ul><ul><li>Occasional freelancer </li></ul><ul><li>Blogger </li></ul><ul><li>Geekest drink frequenter </li></ul><ul><li>@JamieFDHurst </li></ul>
  3. 3. Getting the Framework / * you will need to register!
  4. 4. Installation Copy your downloaded directory somewhere handy... Add this include path into your php.ini file...
  5. 5. Installation Make sure your PATH variable references your bin directory... This allows you to make use of the Zend_Tool command line interface
  6. 6. New Project create project <myproject> Controllers Models Views
  7. 7. New Project If your web server is setup correctly, you should see this! Application is accessed from the public folder
  8. 8. A Quick Zend Overview <ul><li>Models (db-tables, object models) </li></ul><ul><li>Views (helpers, scripts, layouts) </li></ul><ul><li>Controllers (actions, helpers) </li></ul><ul><li>Forms </li></ul><ul><li>Modules </li></ul>
  9. 9. Some Basics <ul><li>All classes named for their folder (e.g. Application_Model_Post in application/models/post.php) </li></ul><ul><li>Every Zend module is autoloaded when needed </li></ul><ul><li>Default controller/action is Index </li></ul><ul><li>URL format: /<controller>/<action> </li></ul>
  10. 10. Database <ul><li>post </li></ul><ul><li>id </li></ul><ul><li>category_id </li></ul><ul><li>title </li></ul><ul><li>content </li></ul><ul><li>time </li></ul><ul><li>category </li></ul><ul><li>id </li></ul><ul><li>name </li></ul><ul><li>comment </li></ul><ul><li>id </li></ul><ul><li>post_id </li></ul><ul><li>author </li></ul><ul><li>content </li></ul><ul><li>time </li></ul>1 * 1 * Edit your application/configs/application.ini file...
  11. 11. Tables and Rows Create a table and row model for each table Table classes fetch and find rows, and perform searches... Row objects handle accessing, updating and deleting the data create db-table <name> <table-name> create model <name>
  12. 12. 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
  13. 13. Controllers Controllers have many actions, and an init() method Action methods end in “Action”
  14. 14. Controllers Each action has a corresponding view script Variables and helpers can be accessed
  15. 15. Displaying Posts Access the posts table and row models and pass the results to the view The fetchall() method allows you to query “ where”s and “ order by”s
  16. 16. Displaying Posts The view script accesses variables passed by the controller Note the clever url helper!
  17. 17. The Result... Ta-da! Posts displaying on your index page! But there’s something missing...
  18. 18. 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 the Post model, making use of the find method
  19. 19. Categories and Comments A custom table method is used... Which uses the where functionality
  20. 20. Categories and Comments And now, with some simple alterations to the view script... The beauty of chaining these objects together...
  21. 21. Categories and Comments The result shows the categories and count of comments!
  22. 22. Layouts Zend provides layouts, which can be used to wrap the entire view in a consistent script Edit your application.ini file to enable them...
  23. 23. Layouts Build 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
  24. 24. Layouts And the result of the layout wrapping:
  25. 25. Actions An action for viewing individual posts would be helpful... create action <action> <controller> A blank action is created...
  26. 26. Individual Posts We’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
  27. 27. Individual Posts Now to build a quick view script for the action...
  28. 28. Individual Posts Now let’s make a quick comment form...
  29. 29. Zend_Form <ul><li>Full extensible support for all HTML elements </li></ul><ul><li>Easy transition to ajax forms using jQuery or other </li></ul><ul><li>Validation </li></ul><ul><li>Decorators </li></ul><ul><li>Customisation </li></ul>
  30. 30. Creating the Form create form <name> Simply add the elements in the init method
  31. 31. Including the Form And the form to the controller and handle when it is submitted Validation is handled very easily in one line of code
  32. 32. Including the Form Simply echoing the form variable displays all the elements and any errors
  33. 33. Completed Form The labels and elements are all rendered correctly using their default decorators Labels and elements wrapped in <dt>s and <dd>s
  34. 34. 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
  35. 35. Creating an RSS Feed The view script for this context is views/scripts/index/index.xml.phtml
  36. 36. RSS Feed And there is our finished feed!
  37. 37. Quick Summary <ul><li>Setting up your project - Zend_Tool </li></ul><ul><li>Database configuration & models </li></ul><ul><li>Controllers & actions </li></ul><ul><li>View scripts </li></ul><ul><li>Forms </li></ul><ul><li>MVC context-switching </li></ul>
  38. 38. Thank you! <ul><li>Feel free to send any Zend-related questions to [email_address] , </li></ul><ul><li>or tweet me @JamieFDHurst . </li></ul>