Saltar al contenido principal
Foundations of Zend Framework 2
By:
Adam Culp
Twitter: @adamculp
https://joind.in/14923
Foundations of Zend Framework 2
 About me
 PHP 5.3 Certified
 Consultant at Zend Technologies
 Organizer SoFloPHP (South Florida)
 Organizer SunshinePHP (Miami)
 Long Distance (ultra) Runner
 Judo Black Belt Instructor
Foundations of Zend Framework 2
 What is...
 Uses PHP >= 5.5
 Open Source
 On GitHub
 Diverse Install
 Pyrus, Composer, Git Submodules
 Built on MVC design pattern
 Can be used as components or entire framework
Foundations of Zend Framework 2
 Skeleton Application
 Git clone Zendframework Skeleton Application
 Github /zendframework/ZendSkeletonApplication
Foundations of Zend Framework 2
 Composer
 Update Composer
 php composer.phar self-update
 Install Zend Framework 2
 php composer.phar install
 Creates and/or populates '/vendor' directory
 Clones Zend Framework 2
 Sets up Composer autoloader (PSR-0)
Foundations of Zend Framework 2
 Composer (easiest)
 Update Composer
 php composer.phar self-update
 Create Project
 php composer create-project
zendframework/skeleton-application
Foundations of Zend Framework 2
 Structure
Foundations of Zend Framework 2
 Zend Framework 2 Usage
 NO MAGIC!!!
 Configuration driven
 No forced structure
 Uses namespaces
Foundations of Zend Framework 2
 MVC
 Very briefly
Foundations of Zend Framework 2
 M = Model
 Model = Business Logic, Data
 Models
 Database
 Entities
 Services
Foundations of Zend Framework 2
 V = View
 View = GUI, Presentation, Visual Representation
 HTML
 CSS
 Javascript
 JSON
 XML
 NO BUSINESS LOGIC!
Foundations of Zend Framework 2
 C = Controller
 Controller = Link between a user and the system.
 Places calls to Model layer.
 Passes needed info to the View layer.
Foundations of Zend Framework 2
 Typical Application Flow - Load
 index.php
 Loads autoloader (PSR-0 = default)
 init Application using application.config.php
Foundations of Zend Framework 2
 Typical Application Flow – App Config
 application.config.php
 Loads modules one at a time
 (Module.php = convention)
 Specifies where to find modules
 Loads configs in autoload directory (DB settings,
etc.)
Foundations of Zend Framework 2
 Modules
 Related for a specific “problem”.
 Logical separation of application functionality
 Reusable
 Removing a module doesn't kill the application
 By convention modules are found in:
 Modules directory
 Vendor directory
 Contains everything specific to given module
Foundations of Zend Framework 2
 Module
 Contents
 PHP Code
 MVC Functionality
 Library Code
 Though better in Application or via Composer
 May not be related to MVC
 View scripts
 Public assets (images, css, javascript)
 More?
Foundations of Zend Framework 2
 Modules
 Easy creation using Zend Skeleton Module
 GitHub /zendframework/ZendSkeletonModule
Foundations of Zend Framework 2
 Typical Application Flow – Modules
 Module.php (convention)
 Makes MvcEvent accessible via onBootstrap()
 Giving further access to Application, Event Manager,
and Service Manager.
 Loads module.config.php
 Specifies autoloader and location of files.
 May define services and wire event listeners as
needed.
Foundations of Zend Framework 2
 Typical Application Flow – Module Config
 module.config.php
 Containers are component specific
 Routes
 Navigation
 Service Manager
 Translator
 Controllers
 View Manager
 Steer clear of Closures (Anonymous Functions)
 Do not cache well within array.
 Less performant (parsed and compiled on every req)
as a factory only parsed when service is used.
Foundations of Zend Framework 2
 Routes
 Carries how controller maps to request
 Types:
 Hostname – 'me.adamculp.com'
 Literal - '/home'
 Method – 'post,put'
 Part – creates a tree of possible routes
 Regex – use regex to match url '/blog/?<id>[0-9]?'
 Scheme – 'https'
 Segment - '/:controller[/:action][/]'
 Query – specify and capture query string params
Foundations of Zend Framework 2
 Route Example
/module/Application/config/module.config.php
Foundations of Zend Framework 2
 Navigation and Sitemaps (optional)
 Driven by configuration.
/module/Application/config/module.config.php
Foundations of Zend Framework 2
 Navigation and Sitemaps (optional)
 Use in Layout or View.
/module/Application/view/layout/layout.phtml
Foundations of Zend Framework 2
 Database
 3 different ways to interact with data:
 DB
 Select
 Table Gateway
 Or use an ORM of your choosing
Foundations of Zend Framework 2
 Services
 ALL THE THINGS!
Foundations of Zend Framework 2
 ServiceManager
 Recommended alternative to ZendDi
 Di pure DIC, SM is factory-based container (no
magic, code explicitly details how instance created)
 Can be created from:
 Application configuration
 Module classes
 Module configuration
 Local override configuration
 Everything is a service, even Controllers (though
provided by ControllerManager)
Foundations of Zend Framework 2
 Service Sample
/module/Application/config/module.config.php
Foundations of Zend Framework 2
 Service Usage
/module/Application/Module.php
/module/Application/view/layout/layout.phtml
Foundations of Zend Framework 2
 Services
 Types:
 Explicit (name => object pairs)
 Invokables (name => class to instantiate)
 Factories (name => callable returning object)
 Aliases (name => some other name)
 Abstract Factories (unknown services)
 Scoped Containers (limit what can be created)
 Shared (or not; you decide)
Foundations of Zend Framework 2
 Event Manager
 Triggers events
 Listen and react to triggered events
 Object that aggregates listeners
 Listener is a callback that can react to an event
 Event is an action
Foundations of Zend Framework 2
 Diagram of MVC Events
Foundations of Zend Framework 2
 Events
 Everything is an event
 loadModule(s)
 .resolve, .post
 bootstrap
 route
 dispatch
 dispatch.error
 render
 render.error
 finish
 sendResponse
Foundations of Zend Framework 2
 Event Sample
/module/Application/Module.php
Foundations of Zend Framework 2
 Views
 Directory Structure
Foundations of Zend Framework 2
 Views
 View Model
 Array carrying info to the view.
 Automatically created, unless created specifically.
 Hold Variable Containers for use in the View.
/module/Application/Module.php
/module/Application/view/layout/layout.phtml
Foundations of Zend Framework 2
 Forms Sample
 Create elements
/module/Products/src/Products/Form/ProductSearchForm.php
Foundations of Zend Framework 2
 Forms Sample
 Pass the form to view
/module/Products/src/Products/Controller/ProductsController.php
Foundations of Zend Framework 2
 Forms Sample
 Output form in view
/module/Products/view/products/products/search.phtml
Foundations of Zend Framework 2
 REST
 AbstractRestfulController
 Parsing JSON request bodies
 View not needed (with Json View Strategy)
 Contains methods to handle get, post, put, delete
 Extend as needed
Foundations of Zend Framework 2
 REST Sample
 Create Route
/module/ProductsRest/config/module.config.php
Foundations of Zend Framework 2
 REST Sample
 Apply Json View Strategy
/module/ProductsRest/config/module.config.php
Foundations of Zend Framework 2
 REST Sample
 Create result as JsonModel
 No view files needed, outputs JSON
/module/ProductsRest/src/Controller/ProductsController.php
Foundations of Zend Framework 2
 Apigility.org
 Ensures well-formed API
 Dev does less work
Foundations of Zend Framework 2
 Resources
 http://framework.zend.com
 http://www.zend.com/en/services/training/course-catal
og/zend-framework-2
 http://www.zend.com/en/services/training/course-cata
log/zend-framework-2-advanced
 http://zendframework2.de/cheat-sheet.html
 http://apigility.org
Foundations of Zend Framework 2
 Thank You!
 Rate this talk: https://joind.in/14923
 Code: https://github.com/adamculp/foundations-zf2-talk
Adam Culp
http://www.geekyboy.com
http://RunGeekRadio.com
Twitter @adamculp