a quick start on zend framework 2

52
© All rights reserved. Zend Technologies, Inc. A quick start on Zend Framework 2 by Enrico Zimuel ([email protected]) Senior Software Engineer Zend Framework Core Team Zend Technologies Ltd 19 th May 2012 Verona (Italy)

Upload: enrico-zimuel

Post on 08-May-2015

23.522 views

Category:

Technology


2 download

DESCRIPTION

In this talk we show the skeleton web application for Zend Framework 2. We introduce the new features of the framework, such as the new MVC layer, the Event Manager, the Dependency Injection and much more. The aim of this talk is how to start to programming using ZF2.

TRANSCRIPT

Page 1: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

A quick start onZend Framework 2

by Enrico Zimuel ([email protected])

Senior Software EngineerZend Framework Core TeamZend Technologies Ltd

19th May 2012 Verona (Italy)

Page 2: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

• Enrico Zimuel (@ezimuel)

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

• PHP Engineer at Zend in the Zend

Framework Core Team

• International speaker about PHP and

computer security topics

• Co-author of the italian book

“PHP Best practices” (FAG edizioni)

• Co-founder of the PUG Torino

About me

Page 3: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

ZF2 in a slide

● New architecture (MVC, Di, Events)● Requirement: PHP 5.3.3● No more CLA (Contributor License Agreement)

● Git (GitHub) instead of SVN● Better performance (new autoload)● Module support● Packaging system (pyrus)

Page 4: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

A new core

● The ZF1 way:

▶ Singleton, Registry, and Hard-Coded Dependencies

● The ZF2 approach:

▶ Aspect Oriented Design and Dependency Injection

Page 5: A quick start on Zend Framework 2

© 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 6: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Releases

● ZF2.0.0beta4 next week!● Goal:

▶ beta5 on June▶ ZF 2.0 RC this summer!!!

Page 7: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Autoloading

Page 8: A quick start on Zend Framework 2

© 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: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

ZF1-Style

require_once 'Zend/Loader/StandardAutoloader.php';

$loader = new Zend\Loader\StandardAutoloader(array( 'fallback_autoloader' => true,));

$loader->register();

Page 10: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

ZF2 NS/Prefix

require_once 'Zend/Loader/StandardAutoloader.php';

$loader = new Zend\Loader\StandardAutoloader();

$loader->registerNamespace( 'My', __DIR__ . '/../library/My') ->registerPrefix( 'Foo_', __DIR__ . '/../library/Foo');

$loader->register();

Page 11: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

ZF2 Class-Map

return array( 'My\Foo\Bar' => __DIR__ . '/Foo/Bar.php',);

require_once 'Zend/Loader/ClassMapAutoloader.php';

$loader = new Zend\Loader\ClassMapAutoloader();

$loader->registerAutoloadMap( __DIR__ . '/../library/.classmap.php');

$loader->register();

.classmap.php

Page 12: A quick start on Zend Framework 2

© 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 13: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Performance improvement

● Compared with the ZF1 autoloader

▶ Class-Maps

show a 25-85% improvement

▶ Namespaces/prefixes

shows 10-40% improvement

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

Page 14: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Dependency Injection

Page 15: A quick start on Zend Framework 2

© 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: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Example

class Bar{ … }

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

Page 17: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Sample definition

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

Page 18: A quick start on Zend Framework 2

© 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 19: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Di by annotation

use Zend\Di\Definition\Annotation as Di;

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

class Bar { ... }

Page 20: A quick start on Zend Framework 2

© 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('Foo'); // contains Bar!

More use cases of Zend\Di:

https://github.com/ralphschindler/zf2-di-use-cases

Page 21: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Event Manager

Page 22: A quick start on Zend Framework 2

© 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 23: A quick start on Zend Framework 2

© 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 24: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

MVC

Page 25: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Event driven architecture

● Everything is an event in the MVC architecture of ZF2

Page 26: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

“A module is a collection of code and other files that solves a more specific atomic

problem of the larger business problem.” (from the ZF2 RFC)

Module definition

Page 27: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Module for ZF2

● The basic unit in a ZF2 applicationis a Module

● Modules are “Plug and play” technology● Modules are simple:

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

Page 28: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Quick startZend Skeleton Application

Page 29: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Zend Skeleton Application

● A simple, skeleton application using the new MVC layer and the module system

● Github:▶ git clone --recursive

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

● This project makes use of Git submodules

● Ready for ZF2.0.0beta4

Page 30: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Folder's tree

config

data

module

public

vendor

Page 31: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Config folder

config

autoload

application.config.php

data

module

public

vendor

Page 32: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Data folder

config

data

cache

module

public

vendor

Page 33: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Module folder

moduleApplication

configmodule.config.php

srcApplication

ControllerIndexController.php

viewindex

index.phtmlModule.phpautoload_classmap.phpautoload_functions.phpautoload_registers.php

Name of the module

Page 34: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Public folder

public

images

js

css

.htaccess

index.php

Page 35: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Vendor folder

config

data

module

public

vendor

ZendFramework

Page 36: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

.htaccess

RewriteEngine OnRewriteCond %{REQUEST_FILENAME} -s [OR]RewriteCond %{REQUEST_FILENAME} -l [OR]RewriteCond %{REQUEST_FILENAME} -dRewriteRule ^.*$ - [NC,L]RewriteRule ^.*$ index.php [NC,L]

Page 37: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

index.php

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

use Zend\Loader\AutoloaderFactory, Zend\ServiceManager\ServiceManager, Zend\Mvc\Service\ServiceManagerConfiguration;

AutoloaderFactory::factory();$config = include 'config/application.config.php';

$serviceManager = new ServiceManager(new ServiceManagerConfiguration($config['service_manager']));$serviceManager->setService('ApplicationConfiguration', $config);$serviceManager->get('ModuleManager')->loadModules();

$serviceManager->get('Application')->bootstrap()->run()->send();

Page 38: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

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

application.config.php

Page 39: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Module.phpnamespace Application;

class Module{ public function getAutoloaderConfig() { return array( 'Zend\Loader\ClassMapAutoloader' => array( __DIR__ . '/autoload_classmap.php', ), 'Zend\Loader\StandardAutoloader' => array( 'namespaces' => array( __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, ), ), ); }

public function getConfig() { return include __DIR__ . '/config/module.config.php'; }}

Page 40: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

autoload_classmap.php

return array(

'Application\Controller\IndexController' =>

__DIR__ . '/src/Application/Controller/IndexController.php',

'Application\Module' =>

__DIR__ . '/Module.php',

);

Page 41: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

module.config.phpreturn array( 'router' => array( 'routes' => array( 'default' => array( 'type' => 'Zend\Mvc\Router\Http\Segment', 'options' => array( 'route' => '/[:controller[/:action]]', 'constraints' => array( 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', ), 'defaults' => array( 'controller' => 'IndexController', 'action' => 'index', ),),), 'home' => array( 'type' => 'Zend\Mvc\Router\Http\Literal', 'options' => array( 'route' => '/', 'defaults' => array( 'controller' => 'IndexController', 'action' => 'index', ),),),),),

Page 42: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

module.config.php (2) 'controller' => array( 'classes' => array( 'IndexController' => 'Application\Controller\IndexController' ), ), 'view_manager' => array( 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'index/index' => __DIR__ . '/../view/index/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ), 'template_path_stack' => array( 'application' => __DIR__ . '/../view', ), ),);

Page 43: A quick start on Zend Framework 2

© 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 44: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Packaging system

Page 45: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Source package

● http://packages.zendframework.com/

● Download or use pyrus, a PEAR2 installer

● Pyrus packages:

▶ Pyrus setup▶ 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 46: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

From ZF1 to ZF2

Page 47: A quick start on Zend Framework 2

© 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 options (one is ZF1)

– MVC: module, event based, dispatchable

– DB: new Zend\Db

– Form: new Zend\Form

Page 48: A quick start on Zend Framework 2

© 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 49: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

How to contribute

Page 50: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

We want you!

● How to contribute:

▶ Write code▶ Documentation▶ Testing▶ Feedbacks/comments

https://github.com/zendframework/zf2

Page 51: A quick start on Zend Framework 2

© 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 2by Rob Allen, http://www.akrabat.com

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

Page 52: A quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Thank you!

● Vote this talk:

▶ https://joind.in/6384● Comments and feedbacks:

[email protected]