zend framework 2 quick start

45
© All rights reserved. Zend Technologies, Inc. Zend Framework 2 quick start by Enrico Zimuel ([email protected]) Senior Software Engineer Zend Framework Core Team Zend Technologies Ltd ZFConf – 21th April 2012 Moscow

Upload: enrico-zimuel

Post on 08-May-2015

16.994 views

Category:

Technology


0 download

DESCRIPTION

Zend Framework 2 quick start presentation at ZFCon 2012 of Moscow

TRANSCRIPT

Page 1: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Zend Framework 2quick start

by Enrico Zimuel ([email protected])

Senior Software EngineerZend Framework Core TeamZend Technologies Ltd

ZFConf – 21th April 2012 Moscow

Page 2: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

• Enrico Zimuel (@ezimuel)

• Software Engineer since 1996– Assembly x86, C/C++, Java, Perl, PHP

• Enjoying PHP since 1999

• PHP Engineer at Zend since 2008

• Zend Framework Core Team from

2011

• B.Sc. Computer Science and

Economics from University of

Pescara (Italy)

About me

Page 3: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

● Overview of ZF2

● The new autoloading system

● Dependency Injection

● Event manager

● The new MVC

● Quick start: ZendSkeletonApplication

● Package system

● From ZF1 to ZF2

Summary

Page 4: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

ZF2 key features

● New architecture (MVC, Di, Events)

● Requirement: PHP 5.3

● No more CLA (Contributor License Agreement)

● Git (GitHub) instead of SVN

● Better performance

● Module management

● Packaging system

Page 5: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

A new core

● The ZF1 way:

▶ Singleton, Registry, and Hard-Coded Dependencies

● The ZF2 approach:

▶ Aspect Oriented Designand Dependency Injection

Page 6: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

New architectural approach

● Methodologies used in the development

– Decoupling (Zend\Di)

– Event driven (Zend\EventManager)

– Standard classes (Zend\Stdlib)● Take advantage of PHP 5.3

▶ Namespace▶ Lambda Functions and Closures▶ Better performance

Page 7: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Autoloading

Page 8: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Autoloading

● No more require_once calls!

● Multiple approaches:

– ZF1-style include_path autoloader

– Per-namespace/prefix autoloading

– Class-map autoloading

Page 9: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Classmap generator

● How to generate the .classmap.php?We provided a command line tool: bin/classmap_generator.php

● Usage is trivial:

$ cd your/library$ php /path/to/classmap_generator.php -w

● Class-Map will be created in .classmap.php

Page 10: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Performance improvement

● Class-Maps show a 25% improvement on the ZF1 autoloader when no acceleration is present

▶ 60-85% improvements when an opcode cache is in place!

● Pairing namespaces/prefixes with specific paths shows >10% gains with no acceleration

▶ 40% improvements when an opcode cache is in place!

Note: The new autoloading system of ZF2 has been ported to ZF 1.12

Page 11: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Dependency Injection

Page 12: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Dependency injection

● How to manage dependencies between objects?

● Dependency injection (Di) is a design pattern whose purpose is to reduce the coupling between software components

It's time for your dependency injection!

Page 13: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Di by construct

class Foo { protected $bar; … public function __construct() { $this->bar= new Bar(); } …}

Without Di

class Foo { protected $bar; … public function __construct(Bar $bar) { $this->bar = $bar; } …}

With Di (construct)

Cons:Difficult to testNo isolationDifficult to reuse code

Pros:Easy to testDecouplingFlexible architecture

Page 14: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Di by setter

class Foo{ protected $bar; … public function setBar(Bar $bar) { $this->bar = $bar; } …}

Page 15: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Zend\Di

● Supports the 3 different injection patterns:

– Constructor

– Interface

– Setter

● Implements a Di Container:

– Manage the dependencies using configuration and annotation

– Provide a compiler to autodiscover classes in a path and create the class definitions, for dependencies

Page 16: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Sample definition

$definition = array( 'Foo' => array( 'setBar' => array( 'bar' => array( 'type' => 'Bar', 'required' => true, ), ), ), );

Page 17: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Using the Di container

use Zend\Di\Di, Zend\Di\Configuration; $di = new Di; $config = new Configuration(array( 'definition' => array('class' => $definition) )); $config->configure($di); $foo = $di->get('Foo'); // contains Bar!

Page 18: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Di by annotation

namespace Example { use Zend\Di\Definition\Annotation as Di;

class Foo { public $bam; /** * @Di\Inject() */ public function setBar(Bar $bar){ $this->bar = $bar; } }

class Bar { }}

Page 19: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Di by annotation (2)

$compiler = new Zend\Di\Definition\CompilerDefinition();$compiler->addDirectory('File path of Foo and Bar');$compiler->compile();

$definitions = new Zend\Di\DefinitionList($compiler);$di = new Zend\Di\Di($definitions);

$baz = $di->get('Example\Foo'); // contains Bar!

More use cases of Zend\Di:https://github.com/ralphschindler/zf2-di-use-cases

Page 20: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Event Manager

Page 21: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Event Manager

● An Event Manager is an object that aggregates listeners for one or more named events, and which triggers events.

● A Listener is a callback that can react to an event.

● An Event is an action.

Page 22: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Example

use Zend\EventManager\EventManager;

$events = new EventManager();$events->attach('do', function($e) { $event = $e->getName(); $params = $e->getParams(); printf( 'Handled event “%s”, with parameters %s', $event, json_encode($params) );});$params = array('foo' => 'bar', 'baz' => 'bat');$events->trigger('do', null, $params);

Page 23: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

MVC

Page 24: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Event driven architecture

● Flow: bootstrap, route, dispatch, response

● Everything is an event in MVC of ZF2

Page 25: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Modules

● The basic unit in a ZF2 MVC application is a Module

● A module is a collection of code and other files that solves a more specific atomic problem of the larger business problem

● Modules are simply:

▶ A namespace▶ Containing a single classfile, Module.php

Page 26: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Quick startZendSkeletonApplication

Page 27: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

ZendSkeletonApplication

● A simple, skeleton application using the ZF2 MVC layer and module systems

● On GitHub:▶ git clone --recursive

git://github.com/zendframework/ZendSkeletonApplication.git

● This project makes use of Git submodules

● Works using ZF2.0.0beta3

Page 28: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Folder tree

config

data

module

vendor

public

.htaccessindex.php

css

images

js

application.config.php

autoload

Application

Module.phpautoload_classmap.phpautoload_function.phpautoload_register.php

config

src

view

Application

Controller

IndexController.php

Page 29: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Output

Page 30: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

index.php

chdir(dirname(__DIR__));require_once (getenv('ZF2_PATH') ?: 'vendor/ZendFramework/library') . '/Zend/Loader/AutoloaderFactory.php';Zend\Loader\AutoloaderFactory::factory();

$appConfig = include 'config/application.config.php';

$listenerOptions = new Zend\Module\Listener\ListenerOptions($appConfig['module_listener_options']);

$defaultListeners = new Zend\Module\Listener\DefaultListenerAggregate($listenerOptions);$defaultListeners->getConfigListener()

->addConfigGlobPath("config/autoload/{,*.}{global,local}.config.php");...

Page 31: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

index.php (2)

...$moduleManager = new Zend\Module\Manager($appConfig['modules']);$moduleManager->events()->attachAggregate($defaultListeners);$moduleManager->loadModules();

// Create application, bootstrap, and run$bootstrap = new Zend\Mvc\Bootstrap(

$defaultListeners->getConfigListener()->getMergedConfig());$application = new Zend\Mvc\Application;$bootstrap->bootstrap($application);$application->run()->send();

Page 32: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

config/application.config.php

return array( 'modules' => array( 'Application' ), 'module_listener_options' => array( 'config_cache_enabled' => false, 'cache_dir' => 'data/cache', 'module_paths' => array( './module', './vendor', ), ),);

Page 33: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

config/autoload

● global.config.php

● local.config.php.dist (.gitignore)

● By default, this application is configured to load all configs in: ./config/autoload/{,*.}{global,local}.config.php.

● Doing this provides a location for a developer to drop in configuration override files provided by modules, as well as cleanly provide individual, application-wide config files for things like database connections, etc.

Page 34: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Application/config/module.config.php

return array( 'di' => array( 'instance' => array(

… ) ));

Here you configure the componentsof your application (i.e. routing,controller, view)

Page 35: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

IndexController.php

namespace Application\Controller;

use Zend\Mvc\Controller\ActionController, Zend\View\Model\ViewModel;

class IndexController extends ActionController{ public function indexAction() { return new ViewModel(); }}

Page 36: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Modules are “plug and play”

● Easy to reuse a module, only 3 steps:

1) Copy the module in \module (or \vendor) folder

2) Enable the module in your application.config.php

▶ Add the name of the module in “modules”

3) Copy the config file of the module in /config/autoload/module.<module's name>.config.php

Page 37: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

modules.zendframework.com

Page 38: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Package system

Page 39: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Package system

● We use Pyrus, a tool to manage PEAR packages. Pyrus simplifies and improves the PEAR experience.

● Source packages (download + github):▶ http://packages.zendframework.com/

● Install and configure pyrus:▶ wget http://packages.zendframework.com/pyrus.phar▶ pyrus.phar .▶ pyrus.phar . channel-discover packages.zendframework.com

● Install a Zend_<component>:▶ pyrus.phar . install zf2/Zend_<component>

Page 40: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

From ZF1 to ZF2

Page 41: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Migrate to ZF2

● Goal: migrate without rewriting much code!

● Main steps

– Namespace: Zend_Foo => Zend\Foo

– Exceptions: an Interface for each components, no more Zend_Exception

– Autoloading: 3 possible options (one is ZF1)

▶ MVC: module, event based, dispatchable

Page 42: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

ZF1 migration prototype

● Source code: http://bit.ly/pvc0X1

● Creates a "Zf1Compat" version of the ZF1 dispatcher as an event listener.

● The bootstrap largely mimics how ZF1's Zend_Application bootstrap works.

● The default route utilizes the new ZF2 MVC routing, but mimics what ZF1 provided.

Page 43: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Helping out

● http://framework.zend.com/zf2

● http://github.com/zendframework

● https://github.com/zendframework/ZendSkeletonApplication

● Getting Started with Zend Framework 2 by Rob Allen, http://www.akrabat.com

● Weekly IRC meetings (#zf2-meeting on Freenode)

● #zftalk.2 on Freenode IRC

Page 44: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Questions?

Page 45: Zend Framework 2 quick start

© All rights reserved. Zend Technologies, Inc.

Thank you!

● Comments and feedbacks:

– Email: [email protected]

– Twitter: @ezimuel