$30 off During Our Annual Pro Sale. View Details »

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.

Michael Scholl

March 05, 2013
Tweet

More Decks by Michael Scholl

Other Decks in Technology

Transcript

  1. App Development with ZF2
    Tired of Album examples?
    Let‘s try a real world problem example…

    View Slide

  2. me
    Michael Scholl
    EasyBib / Wesrc / sch0ll
    twitter: @mischosch
    sch0ll.de / wesrc.com

    View Slide

  3. hu?
    ZF2 MVC
    ZF2 Modules
    ZF2 EventManager
    ZF2 + Doctrine

    no live coding! but a real world problem solution…

    View Slide

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

    View Slide

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

    View Slide

  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“

    View Slide

  7. me
    don‘t like using phone at all!
    (FAX¿ - don‘t get me started about FAX PlugIns for
    my „digital workstation“)

    View Slide

  8. so… we need
    an app for this!
    a key! (idea given from @lenada)

    View Slide

  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

    View Slide

  10. WHAT WE SEE

    View Slide

  11. WHAT WE GET

    View Slide

  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

    View Slide

  13. MODULE FOLDER
    So, module folder contains your own „Modules“
    „Application“ is the only Module right now

    View Slide

  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

    View Slide

  15. Module Folder Structure
    modules
    Namespace
    config
    language
    public
    css
    js
    images
    src
    Namespace
    Controller
    Form
    Service
    ...
    test
    view
    layout
    namespace

    View Slide

  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

    View Slide

  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/

    View Slide

  18. ZF2 Modules
    of course: http://modules.zendframework.com

    View Slide

  19. MODULE.PHP
    Listener
    Listener
    Listener
    Your Module definition

    View Slide

  20. MODULE.CONFIG.PHP - ROUTER

    View Slide

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

    View Slide

  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…

    View Slide

  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!)

    View Slide

  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

    View Slide

  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/

    View Slide

  26. INDEXCONTROLLER

    View Slide

  27. INDEX VIEW TEMPLATE

    View Slide

  28. LAYOUT VIEW TEMPLATE

    View Slide

  29. But we talked about
    „real world example“

    View Slide

  30. may I introduce …

    View Slide

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

    View Slide

  32. Secretary
    application.config.php

    View Slide

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

    View Slide

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

    View Slide

  35. autoload/local.php

    View Slide

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

    View Slide

  37. Secretary
    module.config.php

    View Slide

  38. MODULE.CONFIG.PHP - DOCTRINE & NAVIGATION

    View Slide

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

    View Slide

  40. MODULE.CONFIG.PHP - CONTROLLERS

    View Slide

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

    View Slide

  42. Secretary
    Module.php

    View Slide

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

    View Slide

  44. MODULE.PHP - INIT LOGGER

    View Slide

  45. MODULE.PHP - INIT LOGGER
    ONLY GIVEN IDENTIFIER CAN TRIGGER EVENTS !
    (USE * WILDCARD INSTEAD, IF NEEDED EVERYWHERE)

    View Slide

  46. MODULE.PHP - GET CONFIG & AUTOLOADER

    View Slide

  47. MODULE.PHP - VIEW CONFIG
    SET ACL FOR
    NAVIGATION
    VIEW PLUGIN
    ENABLE
    MARKDOWN
    VIEW PLUGIN
    will be callable by
    $this->SecretaryMarkdown($foo) in view

    View Slide

  48. MODULE.PHP - FORM CONFIG
    THIS ONE IS LITTLE BIT CRAZY
    IF YOU WANT TO USE
    DoctrineORMModule\Form\Element\EntitySelect

    View Slide

  49. so… we need that key!

    View Slide

  50. KEYCONTROLLER.PHP

    View Slide

  51. KEYCONTROLLER.PHP - SETTER & GETTER

    View Slide

  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

    View Slide

  53. KEYCONTROLLER.PHP - ADD ACTION
    redirect, if key already given
    Create
    KeyForm & ViewModel

    View Slide

  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

    View Slide

  55. KEY FORM

    View Slide

  56. KEY ENTITY

    View Slide

  57. KEY VIEW

    View Slide

  58. WHAT YOU GET - INDEX

    View Slide

  59. WHAT YOU GET - ADD

    View Slide

  60. it‘s just that easy ;)

    View Slide

  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/

    View Slide

  62. Secretary proudly presented by
    wesrc.com
    HTTPS://GITHUB.COM/WESRC/SECRETARY

    View Slide

  63. Thank you for listening

    View Slide