Matthew Weier O'Phinney
ServiceManager
, Di
)EventManager
)DispatchableInterface
)How do controllers get their dependencies?
1 <?php
2 array('services' => array(
3 'foo' => new Some\Component\Foo(),
4 ))
1 <?php
2 array('invokables' => array(
3 'foo' => 'Some\Component\Foo',
4 ))
1 <?php
2 array('factories' => array(
3 'foo' => function ($services) {
4 // lazy-loading, essentially:
5 return new Some\Component\Foo();
6 },
7 'bar' => 'Some\Static::method',
8 'baz' => 'Some\Class\Implementing\FactoryInterface',
9 ))
1 <?php
2 array('aliases' => array(
3 'my_foo' => 'foo', // alias services
4 'foo_master' => 'my_foo', // alias aliases
5 ))
1 <?php
2 array('abstract_factories' => array(
3 'Class\Implementing\AbstractFactoryInterface',
4 ))
5
6 class SampleAbstractFactory implements AbstractFactoryInterface
7 {
8 public function canCreateServiceWithName($name) {/* */}
9 public function createServiceWithName(
10 ServiceLocatorInterface $locator, $name
11 ) { /* */ }
12 }
1 <?php
2 array('shared' => array(
3 'EventManager' => false, // default is true
4 ))
ServiceManager
to wire the default workflow and event listeners 1 <?php
2 use Zend\EventManager\EventManager;
3 $events = new EventManager();
4 $events->attach('do', function ($e) {
5 printf(
6 'Handled event "%s" with parameters "%s"'
7 $e->getName(),
8 json_encode($e->getParams())
9 );
10 });
11 $params = array('foo' => 'bar', 'baz' => 'bat');
12 $events->trigger('do', null, $params);
1 <?php
2 namespace Zend\Stdlib;
3
4 interface DispatchableInterface
5 {
6 public function dispatch(
7 RequestInterface $request,
8 ResponseInterface $response = null
9 );
10 }
Controllers are services
A module is all related code and assets that solve a specific problem.
Module
objects to
listeners.Module
.When you write ZF2 MVC applications, you will write Modules.
1 cd my/project/dir
2 git clone \
3 git://github.com/zendframework/ZendSkeletonApplication.git
4 cd ZendSkeletonApplication
5 php composer.phar install
(Live coding here...)
Via composer:
php composer.phar install
"peep": Make a cheeping or beeping sound
(live coding)
1 <?php
2 namespace PhlyPeep;
3
4 class Module {
5 public function getAutoloaderConfig() {
6 return array(
7 'Zend\Loader\StandardAutoloader' => array(
8 'namespaces' => array(
9 'PhlyPeep' => __DIR__ . '/src/PhlyPeep',
10 )
11 )
12 );
13 }
14
15 public function getConfig() {
16 return include __DIR__ . '/config/module.config.php';
17 }
18 }
1 <?php
2 namespace PhlyPeep\Model;
3
4 use Zend\InputFilter\InputFilterAwareInterface;
5 use Zend\InputFilter\InputFilterInterface;
6 use Zend\Stdlib\ArraySerializableInterface;
7
8 class PeepEntity implements
9 ArraySerializableInterface,
10 InputFilterAwareInterface
11 {
12 protected $filter;
13
14 protected $identifier;
15 protected $username;
16 protected $email;
17 protected $displayName;
18 protected $timestamp;
19 protected $peepText;
20 }
1 CREATE TABLE peep
2 (
3 identifier CHARACTER(8) PRIMARY KEY NOT NULL,
4 username VARCHAR(255) NOT NULL,
5 email VARCHAR(255) NOT NULL,
6 display_name VARCHAR(50) DEFAULT NULL,
7 timestamp INTEGER NOT NULL,
8 peep_text TEXT NOT NULL
9 );
10
11 CREATE INDEX peep_username ON peep(username);
getItems($offset, $numPerPage)
and count()
(live coding time)
(live coding time)
View Scripts:
View Helpers
1 {
2 "name": "phly/phly-peep",
3 "description": "Rudimentary twitter clone",
4 "type": "library",
5 "keywords": ["zf2", "zend", "module"],
6 "homepage": "https://github.com/weierophinney/PhlyPeep",
7 "authors": [ /* ... */ ],
8 "require": {
9 "php": ">=5.3.3",
10 "zendframework/zendframework": "dev-master",
11 "zf-commons/zfc-user": "dev-master"
12 },
13 "autoload": {
14 "psr-0": {
15 "PhlyPeep": "src/"
16 },
17 "classmap": [
18 "./"
19 ]
20 }
21 }
1 <?php
2 return array(
3 'modules' => array(
4 'Application',
5 'PhlyContact',
6 'PhlyPeep', // HERE IT IS!
7 'ZfcBase',
8 'ZfcUser',
9 ),
10 /* ... */
11 );
Prefix modules with your vendor name
Define aliases, routes, etc. using a normalized module prefix; point them to
class names or interfaces by default. (phly-peep-service
,
zfcuser_db_adapter
, etc.)
1 <?php
2 return array(
3 'service_manager' => array('aliases' => array(
4 'phly-peep-db-adapter' => 'Zend\Db\Adapter\Adapter',
5 ))
6 );
Helpers and plugins can be looked up via the service manager. Leverage that.
1 <?php
2 return array(
3 'service_manager' => array(
4 'invokables' => array(
5 'peeptext' => 'PhlyPeep\View\PeepText',
6 ),
7 'factories' => array(
8 'peepform' => 'PhlyPeep\Service\PeepViewFormFactory',
9 ),
10 )
11 );
Define a single, top-level route for the module, with children beneath
1 <?php
2 return array('router' => array('routes' => array(
3 'phly-peep' => array(
4 'type' => 'Literal',
5 /* ... */
6 'may_terminate' => true, // or not
7 'child_routes' => array(
8 /* all other routes for this module */
9 ),
10 )
11 )));
Define and delineate dependencies, preferably via a form of package metadata.
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |