DEPRECATED-Please see http://www.slideshare.net/adamculp/foundations-of-zendframework for updated version.
For this talk Adam Culp will cover a basic intro to Zend Framework 2 (ZF2) and how to use the foundational pieces. We will discover how to get a Zend Framework 2 application up and running quickly using GitHub, Composer, and the Zend Framework 2 Skeleton Application. Then we will leverage the Zend Skeleton Module to introduce adding modules to a Zend Framework 2 application.
We will also cover basic usage of the ZF2 module manager, event manager, service manager, and database components. Adam will also introduce some useful resources to help attendees continue learning on their own. The goal of the talk is to give attendees enough information to be able to get a jump start into using ZF2.
1. Foundations of Zend Framework 2
By:
Adam Culp
Twitter: @adamculp
https://joind.in/14923
2. 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
3. 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
10. Foundations of Zend Framework 2
M = Model
Model = Business Logic, Data
Models
Database
Entities
Services
11. Foundations of Zend Framework 2
V = View
View = GUI, Presentation, Visual Representation
HTML
CSS
Javascript
JSON
XML
NO BUSINESS LOGIC!
12. 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.
14. 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.)
15. 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
16. 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?
17. Foundations of Zend Framework 2
Modules
Easy creation using Zend Skeleton Module
GitHub /zendframework/ZendSkeletonModule
18. 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.
19. 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.
20. 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
21. Foundations of Zend Framework 2
Route Example
/module/Application/config/module.config.php
22. Foundations of Zend Framework 2
Navigation and Sitemaps (optional)
Driven by configuration.
/module/Application/config/module.config.php
23. Foundations of Zend Framework 2
Navigation and Sitemaps (optional)
Use in Layout or View.
/module/Application/view/layout/layout.phtml
24. Foundations of Zend Framework 2
Database
3 different ways to interact with data:
DB
Select
Table Gateway
Or use an ORM of your choosing
26. 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)
27. Foundations of Zend Framework 2
Service Sample
/module/Application/config/module.config.php
28. Foundations of Zend Framework 2
Service Usage
/module/Application/Module.php
/module/Application/view/layout/layout.phtml
29. 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)
30. 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
35. 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
36. Foundations of Zend Framework 2
Forms Sample
Create elements
/module/Products/src/Products/Form/ProductSearchForm.php
37. Foundations of Zend Framework 2
Forms Sample
Pass the form to view
/module/Products/src/Products/Controller/ProductsController.php
38. Foundations of Zend Framework 2
Forms Sample
Output form in view
/module/Products/view/products/products/search.phtml
39. 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