Your SlideShare is downloading. ×
0
My Very First…Model View Controller Web Applicationwith the Zend Framework<br />New York CityZF Meetup<br />
Part One<br />Why use an MVC framework?<br />Inversion of Control makes life simpler.<br />Setting up an environment for u...
Why Use an MVC Framework?<br />Helps to stay DRY (don’t repeat others either)<br />Spend time on unique application probl...
Inversion of Control<br />Framework != Library<br />A library is a set of tools.<br />A framework gives you a working stru...
Zend Framework MVC Framework + Components Library + CMD Tool <br />MVC architecture.<br />Useful components for everyday t...
Zend Framework<br />Use at will:<br />Components can be integrated into an app done with Lithium, Symfony, Wordpress, Drup...
           Background Info<br />MVC frameworks typicallyare made of:<br />Controllers and action methods<br />1 Action Met...
           Simple Example(find the code for a URL)<br />In a ZF app, this URL:<br />Maps to the “about action method” of t...
Simple Example<br />The “index controller” is a class.<br />The “about action method” is a method of the IndexController c...
Simple Example<br />About action is called by the framework and renders the “about view script”.<br />
        Simple Example<br />The results of the view script are output.<br />
Simple Example: Summary<br />Framework maps URL to an action method.<br />You write one method and one view script<br />Fr...
     Real Project: Tasks<br />Want an application to create, manage, and view my tasks.<br />Users should be able to regis...
    Tasks: So we’ll need…<br />A user component.<br />A registration component.<br />A tasks component.<br />Security to m...
Set Up 1: ZF Command Line Tool<br />Download current version of ZF<br />Folder will contain Zend library, and bin:<br />Wi...
Set Up 2: Zend library to Include Path<br />Can add in php.ini<br />Can add in application’s index.php file<br />
       Step 1: Create Project<br />Use ZF tool to create project structure.<br />Must “enter” app directory for further to...
Step 1: Create Project<br />“public” is the face of your application.<br />Accessible to civilians<br />CSS, Javascript, o...
Step 1: Create Project<br />zf create project creates an index controller with a default index action and script<br />
Step 2: Modify Index Action Output<br />Want to put something specific to your app on public splash page.<br />Need to:<br...
Step 2: Modify Index Action Output<br />One controller class per file, <br />View scripts housed in views/scripts<br />Gro...
Step 2: Modify Index Action Output<br />Change views/scripts/index/index.phtmlfrom this…<br />
Step 2: Modify Index Action Output<br />…to this:<br />
Step 3: Variables in View Scripts<br />In ZF, view is an object<br />View object is in charge of rendering scripts<br />Vi...
Step 3: Variables in View Scripts<br />Variables assigned as view object properties in controller:<br />
Step 3: Variables in View Scripts<br />Variables used as members of view object inside of view script:<br />
Step 3: Variables in View Scripts<br />
  Step 4: Create Another Page<br />A Page ≈ An Action Method<br />Create action methods using ZF tool<br />Creates method ...
Step 4: Create Another Page<br />Create an about page:<br />
Step 4: Create Another Page<br />Action body and view script magically created!<br />
Step 4: Create Another Page<br />Modify view script as desired and presto!<br />
Steps 5 – 8: Users<br />Want to be able to register users<br />Want to be able to store user data<br />Want to let users m...
Steps 5 – 8: Users<br />So we’ll need:<br />A way to interact with a user database table (Zend_Db)<br />A way for users to...
Steps 5 – 8: Users<br />Start with a Users table like so:<br />
Step 5: Create UserController<br />We’ll put user related actions in the UserController.<br />Use ZF tool to create the co...
Step 5: Create UserController<br />
Step 5: Create UserController<br />
  Step 6: Using a Database<br />ZF has adapter based access toDB tables.<br />Most major DB server’s supported<br />MySQL,...
Step 6: Using a Database<br />2 or 3 Steps:<br />Create DB adapter<br />Optionally, create table<br />Use it<br />
Step 6: Using a Database<br />We’ll use simple version (query against DB)<br />For now, DB config in controller init metho...
Step 6: Using a Database<br />Method to insert is…insert!<br />
Step 6: Using a Database<br />Method to update is…update!<br />
Step 6: Using a Database<br />Method to delete is…delete!<br />
Step 6: Using a Database<br />Various methods for SELECT statements:<br />fetchAll<br />fetchAssoc<br />fetchNum<br />fetc...
Step 6: Using a Database<br />
Step 6: Using a Database<br />Methods are similar when using Zend_Db_Table<br />Usually configure DB in bootstrap<br />
Step 7: Using Forms<br />Users need to be able to register<br />Use Zend_Form for registration form<br />Forms and form el...
ZF Tool and Forms<br />
Add Elements to Forms<br />Element name and type required<br />
Create and Display a Form<br />Create in controller, assign to view, use Zend_Form::render() to display.<br />
Handling Form Submission<br />Request variables held in request object<br />Request object accessed in action<br />Use Zen...
Handling Form Submission<br />Action controls flow, form controls data<br />
Valid Data Submission<br />
Invalid Data Submission<br />
Zend Form<br />Add and configure form elements<br />Use Zend_Form::isValid() to test for data validity<br />Use Zend_Form:...
Step 8 (next time): Access Control<br />Zend_Acl<br />Roles, Resources, Privileges<br />Zend_Auth<br />Action helpers<br /...
New York City area Zend Framework Meetup<br />http://www.meetup.com/ZendFramework-NYCmetro/<br />Affiliated with http://ww...
Upcoming SlideShare
Loading in...5
×

My Very First Zf App Part One

1,227

Published on

First steps to create a basic app with ZF: using action methods, databases, and forms. From February presentation at ZF-NYC meetup. More to follow in March meetup.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,227
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "My Very First Zf App Part One"

  1. 1. My Very First…Model View Controller Web Applicationwith the Zend Framework<br />New York CityZF Meetup<br />
  2. 2. Part One<br />Why use an MVC framework?<br />Inversion of Control makes life simpler.<br />Setting up an environment for use with ZF.<br />Creating a project with ZF tool.<br />ZF application basics.<br />
  3. 3. Why Use an MVC Framework?<br />Helps to stay DRY (don’t repeat others either)<br />Spend time on unique application problems, not plumbing problems.<br />Take advantage of combined years of experience.<br />Things you think of down the line already taken care of.<br />Flexible, reliable, well tested code base:<br />Unit tested.<br />Community tested.<br />
  4. 4. Inversion of Control<br />Framework != Library<br />A library is a set of tools.<br />A framework gives you a working structure to customize by hooking into key points.<br />The framework invokes your code at the appropriate time (control is inverted).<br />
  5. 5. Zend Framework MVC Framework + Components Library + CMD Tool <br />MVC architecture.<br />Useful components for everyday tasks:<br />ACL + Auth<br />Forms: validation, filtering built in<br />Web services: Amazon, Twitter, etc<br />Command line tool eases set-up tasks.<br />
  6. 6. Zend Framework<br />Use at will:<br />Components can be integrated into an app done with Lithium, Symfony, Wordpress, Drupal, etc.<br />Large user community<br />500 contributors, 10 million downloads<br />Corporate backing (+/-)<br />Test driven development<br />
  7. 7. Background Info<br />MVC frameworks typicallyare made of:<br />Controllers and action methods<br />1 Action Method ≈ 1 Page<br />Controllers group action methods<br />View scripts<br />Control look and feel of app<br />Models<br />Application data<br />
  8. 8. Simple Example(find the code for a URL)<br />In a ZF app, this URL:<br />Maps to the “about action method” of the “index controller”<br />
  9. 9. Simple Example<br />The “index controller” is a class.<br />The “about action method” is a method of the IndexController class.<br />
  10. 10. Simple Example<br />About action is called by the framework and renders the “about view script”.<br />
  11. 11. Simple Example<br />The results of the view script are output.<br />
  12. 12. Simple Example: Summary<br />Framework maps URL to an action method.<br />You write one method and one view script<br />Framework calls your code when it’s needed<br />Framework presents view script to user<br />
  13. 13. Real Project: Tasks<br />Want an application to create, manage, and view my tasks.<br />Users should be able to register.<br />Users should be able to create, edit, and delete individual tasks.<br />Users should be able to view a list of their tasks, or any single task.<br />
  14. 14. Tasks: So we’ll need…<br />A user component.<br />A registration component.<br />A tasks component.<br />Security to make sure: <br />Only members can see other users<br />Only members can do task stuff<br />Only the owner of a task can do stuff to her tasks<br />etc<br />
  15. 15. Set Up 1: ZF Command Line Tool<br />Download current version of ZF<br />Folder will contain Zend library, and bin:<br />Windows: Add bin to your PATH<br />*nix: Create symbolic link<br />Even better, use PEAR installer!<br />
  16. 16. Set Up 2: Zend library to Include Path<br />Can add in php.ini<br />Can add in application’s index.php file<br />
  17. 17. Step 1: Create Project<br />Use ZF tool to create project structure.<br />Must “enter” app directory for further tooling.<br />
  18. 18. Step 1: Create Project<br />“public” is the face of your application.<br />Accessible to civilians<br />CSS, Javascript, other asset files go here.<br />index.php is single point of entry.<br />.htaccess automatically created<br />“application” is where most of the app lives.<br />“library” is for app specific libraries.<br />“tests” is for unit tests<br />
  19. 19. Step 1: Create Project<br />zf create project creates an index controller with a default index action and script<br />
  20. 20. Step 2: Modify Index Action Output<br />Want to put something specific to your app on public splash page.<br />Need to:<br />Modify the action method<br />Modify the view script<br />Where are these files in app folder structure?<br />
  21. 21. Step 2: Modify Index Action Output<br />One controller class per file, <br />View scripts housed in views/scripts<br />Grouped into folder named after “it’s controller”<br />
  22. 22. Step 2: Modify Index Action Output<br />Change views/scripts/index/index.phtmlfrom this…<br />
  23. 23. Step 2: Modify Index Action Output<br />…to this:<br />
  24. 24. Step 3: Variables in View Scripts<br />In ZF, view is an object<br />View object is in charge of rendering scripts<br />View scripts execute inside of view object<br />View scripts have same scope as method of view object<br />In other words, in script $this refers to the view object<br />Controller has reference to view object<br />Controller assigns variable to view object for use in view scripts<br />Assign as property of view object<br />
  25. 25. Step 3: Variables in View Scripts<br />Variables assigned as view object properties in controller:<br />
  26. 26. Step 3: Variables in View Scripts<br />Variables used as members of view object inside of view script:<br />
  27. 27. Step 3: Variables in View Scripts<br />
  28. 28. Step 4: Create Another Page<br />A Page ≈ An Action Method<br />Create action methods using ZF tool<br />Creates method body in controller and view script.<br />Zf create action actionNamecontrollerItsIn<br />(Note that you have to have created the controller using ZF tool to create an action in it using the tool)<br />
  29. 29. Step 4: Create Another Page<br />Create an about page:<br />
  30. 30. Step 4: Create Another Page<br />Action body and view script magically created!<br />
  31. 31. Step 4: Create Another Page<br />Modify view script as desired and presto!<br />
  32. 32. Steps 5 – 8: Users<br />Want to be able to register users<br />Want to be able to store user data<br />Want to let users manage their data<br />Want to make sure only users can access user data<br />
  33. 33. Steps 5 – 8: Users<br />So we’ll need:<br />A way to interact with a user database table (Zend_Db)<br />A way for users to interact with information (Zend_Form)<br />User pages (UserController)<br />Log in and access control (Zend_Acl + Zend_Auth)<br />
  34. 34. Steps 5 – 8: Users<br />Start with a Users table like so:<br />
  35. 35. Step 5: Create UserController<br />We’ll put user related actions in the UserController.<br />Use ZF tool to create the controller and actions<br />Zf create controller controllerName<br />Will create controller class file, and view scripts directory<br />
  36. 36. Step 5: Create UserController<br />
  37. 37. Step 5: Create UserController<br />
  38. 38. Step 6: Using a Database<br />ZF has adapter based access toDB tables.<br />Most major DB server’s supported<br />MySQL, Postgre, Oracle, MSSQL, IBM DB2…<br />Query against adapter  easier to make changes to backend without ripple effects in consumer code.<br />Not Zend_Model<br />
  39. 39. Step 6: Using a Database<br />2 or 3 Steps:<br />Create DB adapter<br />Optionally, create table<br />Use it<br />
  40. 40. Step 6: Using a Database<br />We’ll use simple version (query against DB)<br />For now, DB config in controller init method<br />Think of init as constructor hook<br />
  41. 41. Step 6: Using a Database<br />Method to insert is…insert!<br />
  42. 42. Step 6: Using a Database<br />Method to update is…update!<br />
  43. 43. Step 6: Using a Database<br />Method to delete is…delete!<br />
  44. 44. Step 6: Using a Database<br />Various methods for SELECT statements:<br />fetchAll<br />fetchAssoc<br />fetchNum<br />fetchObj<br />
  45. 45. Step 6: Using a Database<br />
  46. 46. Step 6: Using a Database<br />Methods are similar when using Zend_Db_Table<br />Usually configure DB in bootstrap<br />
  47. 47. Step 7: Using Forms<br />Users need to be able to register<br />Use Zend_Form for registration form<br />Forms and form elements in ZF are objects:<br />Add elements to form with addElement<br />Can add validators to elements to ensure data integrity<br />EmailAddressvalidator<br />Integer validator<br />Many more<br />
  48. 48. ZF Tool and Forms<br />
  49. 49. Add Elements to Forms<br />Element name and type required<br />
  50. 50. Create and Display a Form<br />Create in controller, assign to view, use Zend_Form::render() to display.<br />
  51. 51. Handling Form Submission<br />Request variables held in request object<br />Request object accessed in action<br />Use Zend_Controller_Request::isPost to test for form submission<br />Use Zend_Form::isValid() to test data validity<br />Use Zend_Form::persistData() to save changes<br />This is what I do, other ways exist<br />
  52. 52. Handling Form Submission<br />Action controls flow, form controls data<br />
  53. 53. Valid Data Submission<br />
  54. 54. Invalid Data Submission<br />
  55. 55. Zend Form<br />Add and configure form elements<br />Use Zend_Form::isValid() to test for data validity<br />Use Zend_Form::persistData() to commit changes<br />Note that step 3 can be done other ways<br />
  56. 56. Step 8 (next time): Access Control<br />Zend_Acl<br />Roles, Resources, Privileges<br />Zend_Auth<br />Action helpers<br />Basics<br />Use with access control<br />View helpers<br />Basics<br />Common helpers<br />
  57. 57. New York City area Zend Framework Meetup<br />http://www.meetup.com/ZendFramework-NYCmetro/<br />Affiliated with http://www.nyphp.org/<br />Thanks for attending “Your First Zend Framework Project” <br />presented on Feb. 22, 2011 by<br />Isaac Foster<br />http://www.linkedin.com/in/isaaczfosterisaac.z.foster@gmail.com<br />Alan Seiden<br />http://www.alanseiden.comalan@alanseiden.comTwitter: @alanseiden<br />Sign up to hear about all our ZF meetups at http://www.meetup.com/ZendFramework-NYCmetro/<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×