App Development with ZF2 - A real world problem example

App Development with ZF2 - A real world problem example

Talk for BEPHPUG about app development with Zend Framework 2.

https://github.com/wesrc/secretary is used as example, to demonstrate, how a ZF2 MVC App could look like.

210067c457cce3f33a97629526612489?s=128

Michael Scholl

March 05, 2013
Tweet

Transcript

  1. App Development with ZF2 Tired of Album examples? Let‘s try

    a real world problem example…
  2. me Michael Scholl EasyBib / Wesrc / sch0ll twitter: @mischosch

    sch0ll.de / wesrc.com
  3. hu? ZF2 MVC ZF2 Modules ZF2 EventManager ZF2 + Doctrine

    … no live coding! but a real world problem solution…
  4. But first… Let us assume, we have a problem!

  5. But first… Let us assume, we have a problem!

  6. of course… YOU have no problem, because you always send

    credentials by FAX to your peers & customers! or YOU are the person who is having great fun, telling your customers credentials on the phone and using passwords like this: „B3y3^zA#](}7j6#tmJ=[r“
  7. me don‘t like using phone at all! (FAX¿ - don‘t

    get me started about FAX PlugIns for my „digital workstation“)
  8. so… we need an app for this! a key! (idea

    given from @lenada)
  9. let‘s do that with ZF2 of course, we use Composer

    ! (and ZF community fortunately decided to do so, too) cd ~/Sites/zf2test git clone git://github.com/zendframework/ ZendSkeletonApplication.git cd ZendSkeletonApplication php composer.phar install or php composer.phar create-project --repository-url="http:// packages.zendframework.com" zendframework/skeleton- application ~/Sites/zf2test
  10. WHAT WE SEE

  11. WHAT WE GET

  12. APPLICATION.CONFIG.PHP modules: a list of modules you want to use

    module_listener_options: where to find your modules & how to load other config files by pattern
  13. MODULE FOLDER So, module folder contains your own „Modules“ „Application“

    is the only Module right now
  14. ZF2 Modules can be pretty anything ([more or less] code,

    assets, configs) there is no structure you have to follow but there is a structure, you could/should follow you need a Module.php in the module root Module.php needs its own Namespace
  15. Module Folder Structure modules Namespace config language public css js

    images src Namespace Controller Form Service ... test view layout namespace
  16. ZF2 ModuleManager „A flexible and powerful system for loading managing

    modules“ (Evan Coury) Loads all your modules and fires some listeners AutoloaderListener ConfigListener InitTrigger OnBootstrapListener LocatorRegistrationListener ServiceListener Makes use of ZF2 EventManager
  17. ZF2 Modules more background info: http://www.zend.com/en/resources/webinars/framework?#ZF2ModMan (Webinar with Evan Coury)

    http://blog.evan.pro/introduction-to-modules-in-zend-framework-2-talk- at-zendcon-2012 http://evan.pro/slides/Introduction-to-Modules-in-Zend-Framework-2- ZendCon-2012.html#slide1 http://akrabat.com/zend-framework-2/modules-in-zf2/
  18. ZF2 Modules of course: http://modules.zendframework.com

  19. MODULE.PHP Listener Listener Listener Your Module definition

  20. MODULE.CONFIG.PHP - ROUTER

  21. MODULE.CONFIG.PHP - SERVICES/CONTROLLERS/VIEWS

  22. ZF2 ServiceManager because we can write $sm then! because everybody

    right now uses a DIC your module.config.php will feed the ServiceManager and get you up and running I assume, you already understand the concepts behind DIC (Dependency Injection Container) and IoC (Inversion of Control), if not…
  23. ZF2 ServiceManager Background Info: http://blog.astrumfutura.com/2011/10/zend-framework-2-0- dependency-injection-part-1/ - http://blog.astrumfutura.com/2011/10/ zend-framework-2-0-dependency-injection-part-2/ (must

    read!) http://www.maltblue.com/articles-2/zend-framework-2-core-concepts- understanding-dependency-injection http://www.maltblue.com/tutorial/zend-framework-2-servicemanager http://ralphschindler.com/2012/10/10/di-dic-service-locator-redux http://akrabat.com/zend-framework-2/zendservicemanager- configuration-keys/ (helpful doc article!) http://juriansluiman.nl/en/article/120/using-zend-framework-service- managers-in-your-application (great article!)
  24. ZF2 EventManager ZF2 MVC makes heavy usage of EventManager Everything

    is bound to an event If you want to hook into any bootstrapping process, you will end in attaching to an event You can use the EventManager to integrate event based logic into your own app
  25. ZF2 EventManager Background Info: http://www.mwop.net/blog/266-Using-the-ZF2-EventManager.html http://akrabat.com/zend-framework-2/a-list-of-zf2-events/ http://framework.zend.com/manual/2.1/en/modules/zend.event- manager.event-manager.html http://www.zend.com/en/resources/webinars/framework?#ZF2EM (Webinar

    with Matthew Weier O'Phinney) http://robertbasic.com/blog/azend-framework-2-eventmanager-use- case/
  26. INDEXCONTROLLER

  27. INDEX VIEW TEMPLATE

  28. LAYOUT VIEW TEMPLATE

  29. But we talked about „real world example“

  30. may I introduce …

  31. APP URL LICENSE HTTPS://GITHUB.COM/WESRC/SECRETARY MIT SECRETARY

  32. Secretary application.config.php

  33. APPLICATION.CONFIG.PHP QUITE MORE REAL WORLD EXAMPLE, ISN‘T IT?

  34. APPLICATION.CONFIG.PHP / COMPOSER.JSON USE VERSION NUMBERS!

  35. autoload/local.php

  36. GLOBAL.PHP / LOCAL.PHP DB SETTINGS LANGUAGE SETTINGS

  37. Secretary module.config.php

  38. MODULE.CONFIG.PHP - DOCTRINE & NAVIGATION

  39. MODULE.CONFIG.PHP - ROUTER & SERVICE MANAGER

  40. MODULE.CONFIG.PHP - CONTROLLERS

  41. MODULE.CONFIG.PHP - VIEW TO ENABLE JSON VIEW MODELS

  42. Secretary Module.php

  43. MODULE.PHP - INTERFACES & ONBOOTSTRAP INTERFACES TELL YOU, WHAT TO

    DO!
  44. MODULE.PHP - INIT LOGGER

  45. MODULE.PHP - INIT LOGGER ONLY GIVEN IDENTIFIER CAN TRIGGER EVENTS

    ! (USE * WILDCARD INSTEAD, IF NEEDED EVERYWHERE)
  46. MODULE.PHP - GET CONFIG & AUTOLOADER

  47. MODULE.PHP - VIEW CONFIG SET ACL FOR NAVIGATION VIEW PLUGIN

    ENABLE MARKDOWN VIEW PLUGIN will be callable by $this->SecretaryMarkdown($foo) in view
  48. MODULE.PHP - FORM CONFIG THIS ONE IS LITTLE BIT CRAZY

    IF YOU WANT TO USE DoctrineORMModule\Form\Element\EntitySelect
  49. so… we need that key!

  50. KEYCONTROLLER.PHP

  51. KEYCONTROLLER.PHP - SETTER & GETTER

  52. KEYCONTROLLER.PHP - PRESDISPATCH & INDEX PREDISPATCH IS A OWN EVENT!

    (that is created in MvcController) add key-de_DE.mo and key-us_US.mo to translator Check, if key is already created. If not, create KeyForm. Return keyRecord and keyForm as ViewModel Attributes
  53. KEYCONTROLLER.PHP - ADD ACTION redirect, if key already given Create

    KeyForm & ViewModel
  54. KEYCONTROLLER.PHP - ADD ACTION If Request is POST Create new

    KeyRecord and get Input Filter out of it. Fill Form with POST data If form is valid Get passphrase out of form values Generate Private Key Save Public Key for User ($this->identity) Add Private Key to ViewModel Update User Role
  55. KEY FORM

  56. KEY ENTITY

  57. KEY VIEW

  58. WHAT YOU GET - INDEX

  59. WHAT YOU GET - ADD

  60. it‘s just that easy ;)

  61. ZF2 - More Infos http://framework.zend.com/manual/2.1/en/index.html http://www.zend.com/en/resources/webinars/framework http://akrabat.com/ http://blog.astrumfutura.com/ http://blog.evan.pro http://www.mwop.net

    http://ralphschindler.com http://www.zimuel.it/en/ ZF MailingList: http://framework.zend.com/wiki/display/ZFDEV/Mailing+Lists GITHUB: https://github.com/zendframework/zf2 IRC: http://www.zftalk.com/channel/
  62. Secretary proudly presented by wesrc.com HTTPS://GITHUB.COM/WESRC/SECRETARY

  63. Thank you for listening