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.
4. Installation Copy your downloaded directory somewhere handy... Add this include path into your php.ini file...
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. New Project zf.sh create project <myproject> Controllers Models Views
7. New Project If your web server is setup correctly, you should see this! Application is accessed from the public folder
8.
9.
10.
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 zf.sh create db-table <name> <table-name> zf.sh create model <name>
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
14. Controllers Each action has a corresponding view script Variables and helpers can be accessed
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. Displaying Posts The view script accesses variables passed by the controller Note the clever url helper!
17. The Result... Ta-da! Posts displaying on your index page! But there’s something missing...
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
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. 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
25. Actions An action for viewing individual posts would be helpful... zf.sh create action <action> <controller> A blank action is created...
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
30. Creating the Form zf.sh create form <name> Simply add the elements in the init method
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. Including the Form Simply echoing the form variable displays all the elements and any errors
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. 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. Creating an RSS Feed The view script for this context is views/scripts/index/index.xml.phtml