ZEND FRAMEWORK: GETTING        TO GRIPS         Ryan Mauger
WHO IS RYAN MAUGER?• Zend   Framework Contributor• Zend   Framework CR Team Member• Co-Author     of Zend Framework 2 in A...
WHAT ARE YOU GOING TO       TAKE AWAYFundamental concepts to help you figure things out                 for yourself
WHERE TO START?• Tutorials  • Akrabat’s    (Rob Allen): http://akrabat.com/zft  • Official   Quickstart: http://bit.ly/zf-q...
WHATS NEXT?• Dispatch   cycle• Autoloaders, Plugin   Loaders & Resource Loaders• Plugins• Helpers• Models• Forms, Decorato...
BUT WHAT SHOULD I   TACKLE FIRST?
BUT WHAT SHOULD I   TACKLE FIRST?• Understand   the Lifecycle of a ZF request
BUT WHAT SHOULD I   TACKLE FIRST?• Understand   the Lifecycle of a ZF request• Understand   the Lifecycle of a ZF request
BUT WHAT SHOULD I   TACKLE FIRST?• Understand   the Lifecycle of a ZF request• Understand   the Lifecycle of a ZF request•...
BUT WHAT SHOULD I   TACKLE FIRST?• Understand   the Lifecycle of a ZF request• Understand   the Lifecycle of a ZF request•...
EXECUTION LIFECYCLE       Bootstrap        Route       Dispatch       Simple?
HOW ABOUT A FLOWCHART?Source: Polly Wong http://www.slideshare.net/polleywong/zend-framework-dispatch-workflow
YIKES!
SOMETHING SIMPLER           Bootstrap         routeStartup             route        routeShutdown      dispatchLoopStartup...
SOMETHING SIMPLER   FC Plugin    routeStartup                         Router                            route   routeShutd...
BOOTSTRAPPING
BOOTSTRAPPING• Initialise   everything you may need• Make things ready for your request to be dispatched• Do   nothing mod...
MODULE BOOTSTRAPSresources.frontController.moduleDirectory = APPLICATION_PATH "/modules"resources.frontController.controll...
FRONT CONTROLLERPLUGINS AND ACTION      HELPERS
FRONT CONTROLLER                 PLUGINS• Provide    hooks into various points in the request lifecycle• Run Automatically...
ADDING A FRONT                CONTROLLER PLUGIN• In the config  autoloaderNamespaces[] = " My_ "  resources.frontController...
ACTION HELPERS• Provide   hooks into the request lifecycle• Run   automatically, or on demand• Are intended to either repl...
ADDING AN ACTION HELPER• In   the config resources.frontController.actionHelperPaths.My_Action_Helper = "My/Action/Helper"•...
ACTION HELPER OR FC              PLUGIN?                                   STARTFront Controller Plugin                   ...
AUTOLOADING
AUTOLOADING  Autoloading
AUTOLOADING  Autoloading Plugin loading
AUTOLOADING   Autoloading  Plugin loading Resource loading
AUTOLOADING• Library   components• Follows   PEAR naming• Used   where ever you see: • new   Zend_Form() • new   Zend_Db()...
PLUGIN LOADING• Prefixes   names to resolve a classname, and path to load• Can   work away from the include_path• Is   used...
RESOURCE LOADING• Resolves  classnames which do not map 1:1 with the  filesystem  Application_Model_Page : application/mode...
LOADERS IN ACTION                            Plugin Loading <?php class My_Form extends Zend_Form { 	 public function init...
LOADERS IN ACTION                    Adding your own prefix path   <?php   class My_Form extends Zend_Form   {   	 public f...
LOADERS IN ACTION                      Autoloading  [production]  phpSettings.display_startup_errors = 0  phpSettings.disp...
LOADERS IN ACTION                           Resource Loading<?phpclass Admin_IndexController extends                  Zend...
CONSTRUCTOR OPTIONS    What can I put in $options?
WHAT CAN I PUT IN                 $OPTIONS?•   $options is always an associative array•   Each key is normalised and turne...
WHERE CAN I SEE THE          AVAILABLE SETTERS?• The API   documentation (http://framework.zend.com/apidoc/ core)• Your   ...
ADVANTAGES OF USING         OPTIONS ARRAYS• Flexibility   of configuration• Easily   extended• Largely   self documenting
TAKING ADVANTAGE OF THE OPTIONS ARRAY IN ZEND_FORM<?php                                                                   ...
DECORATORS
HOW THE HECK DODECORATORS WORK THEN?Label <dt><label></label></dt>      Rendered                                   inside-...
DEFINING A DECORATOR        STACK	    	   $multi->setMultiOptions($this->_page->getOptions())	    	         ->setDecorator...
DEFINING A DECORATOR        STACK       BeachPHP Demo
THE M IN YOUR MVC
MODELLING DATA• Business   logic• Domain     logic• Services• Mappers• Entities• Models
ApplicationFront Controller   Domain logic   RDBMS   Action  Controller     View
Zend_Db_Table based models                        DIRECT TDG Domain?                          RDBMSZend_Db_Table
Zend_Db_Table based models                                 TDG WRAPPER BASED         Domain                  MODEL        ...
DataMapper based models                               QUICKSTART MAPPER         Domain             Mapper                 ...
DataMapper based models                                   DOCTRINE2     Domain                                    DBALEnti...
WHICH PATTERN TO USE?• Maintenance   cycle• Complexity   of the application• Project   timeframe• Available        solutio...
WHAT DOES ZF SUPPORT?       DB Abstraction
WHAT DOES ZF SUPPORT?                     DB AbstractionExpect some sort of integration with Doctrine2 when ZF2           ...
THANKS FOR LISTENING       Questions?
Upcoming SlideShare
Loading in...5
×

Webinar: Zend framework Getting to grips (ZF1)

3,047

Published on

Updated only very slightly from when I gave this talk at phpnw.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,047
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
71
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Webinar: Zend framework Getting to grips (ZF1)

    1. 1. ZEND FRAMEWORK: GETTING TO GRIPS Ryan Mauger
    2. 2. WHO IS RYAN MAUGER?• Zend Framework Contributor• Zend Framework CR Team Member• Co-Author of Zend Framework 2 in Action (With Rob Allen)• Technical Editor for Zend Framework: A Beginners Guide• Community Supporter• Zend Certified PHP5 Engineer• Lead Developer at Lupimedia
    3. 3. WHAT ARE YOU GOING TO TAKE AWAYFundamental concepts to help you figure things out for yourself
    4. 4. WHERE TO START?• Tutorials • Akrabat’s (Rob Allen): http://akrabat.com/zft • Official Quickstart: http://bit.ly/zf-quickstart• Build your own sandbox • KEEP IT! • Add to it, keep additions for later reference
    5. 5. WHATS NEXT?• Dispatch cycle• Autoloaders, Plugin Loaders & Resource Loaders• Plugins• Helpers• Models• Forms, Decorators, Validators & Filters
    6. 6. BUT WHAT SHOULD I TACKLE FIRST?
    7. 7. BUT WHAT SHOULD I TACKLE FIRST?• Understand the Lifecycle of a ZF request
    8. 8. BUT WHAT SHOULD I TACKLE FIRST?• Understand the Lifecycle of a ZF request• Understand the Lifecycle of a ZF request
    9. 9. BUT WHAT SHOULD I TACKLE FIRST?• Understand the Lifecycle of a ZF request• Understand the Lifecycle of a ZF request• Understand the Lifecycle of a ZF request
    10. 10. BUT WHAT SHOULD I TACKLE FIRST?• Understand the Lifecycle of a ZF request• Understand the Lifecycle of a ZF request• Understand the Lifecycle of a ZF request• Understand the Lifecycle of a ZF request
    11. 11. EXECUTION LIFECYCLE Bootstrap Route Dispatch Simple?
    12. 12. HOW ABOUT A FLOWCHART?Source: Polly Wong http://www.slideshare.net/polleywong/zend-framework-dispatch-workflow
    13. 13. YIKES!
    14. 14. SOMETHING SIMPLER Bootstrap routeStartup route routeShutdown dispatchLoopStartup preDispatch dispatch (action) postDispatch dispatchLoopShutdown
    15. 15. SOMETHING SIMPLER FC Plugin routeStartup Router route routeShutdown dispatchLoopStartup Controller preDispatch preDispatch Dispatch Loop dispatch (action) postDispatch postDispatchdispatchLoopShutdown
    16. 16. BOOTSTRAPPING
    17. 17. BOOTSTRAPPING• Initialise everything you may need• Make things ready for your request to be dispatched• Do nothing module specific• Remember your module bootstraps, even if they are empty!
    18. 18. MODULE BOOTSTRAPSresources.frontController.moduleDirectory = APPLICATION_PATH "/modules"resources.frontController.controllerDirectory.default = APPLICATION_PATH "/controllers"resources.modules[] = "" <?php class Abcd_Bootstrap extends Zend_Application_Module_Bootstrap { protected function _initRest() { $fc = Zend_Controller_Front::getInstance(); $restRoute = new Zend_Rest_Route($fc, array(), array( abcd => array(contacts), )); $fc->getRouter()->addRoute(contacts, $restRoute); } }
    19. 19. FRONT CONTROLLERPLUGINS AND ACTION HELPERS
    20. 20. FRONT CONTROLLER PLUGINS• Provide hooks into various points in the request lifecycle• Run Automatically• Should be able to run independently of the action controller itself• Exceptionsthrown in preDispatch will not prevent further plugins preDispatch calls being run• Are easier to use if you have no constructor parameters.
    21. 21. ADDING A FRONT CONTROLLER PLUGIN• In the config autoloaderNamespaces[] = " My_ " resources.frontController.plugins[] = "My_Controller_Plugin";• In the bootstrap (useful for modules) <?php class ModuleName_Bootstrap extends Zend_Application_Module_Bootstrap { protected function _initPlugins() { $this->getApplication() ->getResourcePlugin(frontController) ->registerPlugin(new ModuleName_Plugin_Acl()); } }
    22. 22. ACTION HELPERS• Provide hooks into the request lifecycle• Run automatically, or on demand• Are intended to either replace repeated code in your actions (think DRY) or to extend functionality of action controllers• Thrown Exceptions in pre/postDispatch will stop further execution of other action helpers
    23. 23. ADDING AN ACTION HELPER• In the config resources.frontController.actionHelperPaths.My_Action_Helper = "My/Action/Helper"• In the bootstrap (useful for modules) <?php class Abcd_Bootstrap extends Zend_Application_Module_Bootstrap { protected function _initActionHelpers() { Zend_Controller_Action_HelperBroker::addPath( My/Action/Helper, My_Action_Helper ); Zend_Controller_Action_HelperBroker::addHelper( new My_Action_Helper_Thingy() ); } }
    24. 24. ACTION HELPER OR FC PLUGIN? STARTFront Controller Plugin Action Helper Do you need toError Handler interact with it from the controller? Yes Redirector Layout Flash Messenger No Action stack Context Switch Do you need to Yes hook earlier than preDispatch? No View Renderer
    25. 25. AUTOLOADING
    26. 26. AUTOLOADING Autoloading
    27. 27. AUTOLOADING Autoloading Plugin loading
    28. 28. AUTOLOADING Autoloading Plugin loading Resource loading
    29. 29. AUTOLOADING• Library components• Follows PEAR naming• Used where ever you see: • new Zend_Form() • new Zend_Db() • new Zend_Service_...
    30. 30. PLUGIN LOADING• Prefixes names to resolve a classname, and path to load• Can work away from the include_path• Is used wherever a class is created with only a suffix• FormElements, View Helpers, Action Helpers, Resource plugins
    31. 31. RESOURCE LOADING• Resolves classnames which do not map 1:1 with the filesystem Application_Model_Page : application/models/Page.php• Is used for module components (forms, models, services)• Provide a namespace for the module• Iscreated automatically for modules with a module bootstrap• Makes your modules directories ‘tidier’ (controllers, views, models, forms, etc)
    32. 32. LOADERS IN ACTION Plugin Loading <?php class My_Form extends Zend_Form { public function init() { $this->addElement(Text, aTextBox, array(label => A text Box)); } }“Text” is resolved to Zend_Form_Element_Text by looping through the given prefix paths until a match is found
    33. 33. LOADERS IN ACTION Adding your own prefix path <?php class My_Form extends Zend_Form { public function init() { $this->addElementPrefixPath(My_Form_Element_, My/Form/Elements/); $this->addElement(Text, aTextBox, array(label => A text Box)); } } Zend_Form::addElementPrefixPath() is an example of accessing a resource loader to add an extra prefix path.There is also an optional third parameter, to specifically supply a path for only elements, decorators, filters, or validators
    34. 34. LOADERS IN ACTION Autoloading [production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 autoloaderNamespaces[] = "My_" includePaths.library = APPLICATION_PATH "/../library" bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap"<?phpclass IndexController extends Zend_Controller_Action{ public function indexAction() { $myComponent = new My_Cool_Component(); }}
    35. 35. LOADERS IN ACTION Resource Loading<?phpclass Admin_IndexController extends Zend_Controller_Action{ public function indexAction() { $form = new Admin_Form_EditPage(); $this->view->form = $form; }}
    36. 36. CONSTRUCTOR OPTIONS What can I put in $options?
    37. 37. WHAT CAN I PUT IN $OPTIONS?• $options is always an associative array• Each key is normalised and turned into a method name for a setter. ‘foo’ becomes ‘setFoo’, ‘bar’ becomes ‘setBar’• If that setter exists, it is called, and the value at that index is passed as a single argument• Generally, exceptions are not thrown for invalid options.• Some components will store any options without a setter for other purposes, e.g. Zend_Form, options without a setter become attributes
    38. 38. WHERE CAN I SEE THE AVAILABLE SETTERS?• The API documentation (http://framework.zend.com/apidoc/ core)• Your IDE (autocomplete)• The manual
    39. 39. ADVANTAGES OF USING OPTIONS ARRAYS• Flexibility of configuration• Easily extended• Largely self documenting
    40. 40. TAKING ADVANTAGE OF THE OPTIONS ARRAY IN ZEND_FORM<?php setPage() is calledclass Admin_Form_EditPage extends Zend_Form{ protected $_page; before init(), allowing public function init() you to pass through the { $this->addElements(array( constructor an object, //... $multi = new Zend_Form_Element_Select(multi), //... array, or scalar value to )); be used for any } $multi->setMultiOptions($this->_page->getOptions()); purpose when public function setPage(Application_Model_Page $page) initialising your form. { $this->_page = $page; return $this; }}
    41. 41. DECORATORS
    42. 42. HOW THE HECK DODECORATORS WORK THEN?Label <dt><label></label></dt> Rendered inside-out, HtmlTag <dd>...</dd> each wrapping, appending or Element <input.../> prepending Description <p>...</p> content to the content from Errors <ul>...</ul> the previous decorator
    43. 43. DEFINING A DECORATOR STACK $multi->setMultiOptions($this->_page->getOptions()) ->setDecorators(array( ViewHelper, Description, Errors, array(HtmlTag, array(tag => dd)), array(Label, array(tag => dt)), )); ViewHelper - Renders the element Description - renders a paragraph beneath the element (if a description is set) Errors - Adds a ul beneath the element HtmlTag - Renders the dd element Label - Renders the label and dt tags
    44. 44. DEFINING A DECORATOR STACK BeachPHP Demo
    45. 45. THE M IN YOUR MVC
    46. 46. MODELLING DATA• Business logic• Domain logic• Services• Mappers• Entities• Models
    47. 47. ApplicationFront Controller Domain logic RDBMS Action Controller View
    48. 48. Zend_Db_Table based models DIRECT TDG Domain? RDBMSZend_Db_Table
    49. 49. Zend_Db_Table based models TDG WRAPPER BASED Domain MODEL RDBMSEntity Zend_Db_Table
    50. 50. DataMapper based models QUICKSTART MAPPER Domain Mapper RDBMSEntity Zend_Db_Table
    51. 51. DataMapper based models DOCTRINE2 Domain DBALEntity Mapper RDBMS
    52. 52. WHICH PATTERN TO USE?• Maintenance cycle• Complexity of the application• Project timeframe• Available solutions (Doctrine, Propel, phpDataMapper), do they suit you?
    53. 53. WHAT DOES ZF SUPPORT? DB Abstraction
    54. 54. WHAT DOES ZF SUPPORT? DB AbstractionExpect some sort of integration with Doctrine2 when ZF2 arrivesDoctrine2 is gaining popularity as the ORM of choice for ZF on the whole Doctrine 1.x is already a very common choice of ORM, though based on Active Record
    55. 55. THANKS FOR LISTENING Questions?
    1. A particular slide catching your eye?

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

    ×