a quick start on zend framework 2

Post on 08-May-2015

23.522 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

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

© All rights reserved. Zend Technologies, Inc.

A quick start onZend Framework 2

by Enrico Zimuel (enrico@zend.com)

Senior Software EngineerZend Framework Core TeamZend Technologies Ltd

19th May 2012 Verona (Italy)

© 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

© 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)

© 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

© 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

© All rights reserved. Zend Technologies, Inc.

Releases

● ZF2.0.0beta4 next week!● Goal:

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

© All rights reserved. Zend Technologies, Inc.

Autoloading

© 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

© 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();

© 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();

© 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

© 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

© 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

© All rights reserved. Zend Technologies, Inc.

Dependency Injection

© 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

© All rights reserved. Zend Technologies, Inc.

Example

class Bar{ … }

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

© All rights reserved. Zend Technologies, Inc.

Sample definition

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

© 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!

© 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 { ... }

© 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

© All rights reserved. Zend Technologies, Inc.

Event Manager

© 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.

© 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);

© All rights reserved. Zend Technologies, Inc.

MVC

© All rights reserved. Zend Technologies, Inc.

Event driven architecture

● Everything is an event in the MVC architecture of ZF2

© 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

© 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

© All rights reserved. Zend Technologies, Inc.

Quick startZend Skeleton Application

© 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

© All rights reserved. Zend Technologies, Inc.

Folder's tree

config

data

module

public

vendor

© All rights reserved. Zend Technologies, Inc.

Config folder

config

autoload

application.config.php

data

module

public

vendor

© All rights reserved. Zend Technologies, Inc.

Data folder

config

data

cache

module

public

vendor

© 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

© All rights reserved. Zend Technologies, Inc.

Public folder

public

images

js

css

.htaccess

index.php

© All rights reserved. Zend Technologies, Inc.

Vendor folder

config

data

module

public

vendor

ZendFramework

© 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]

© 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();

© 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

© 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'; }}

© 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',

);

© 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', ),),),),),

© 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', ), ),);

© 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(); }}

© All rights reserved. Zend Technologies, Inc.

Packaging system

© 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>

© All rights reserved. Zend Technologies, Inc.

From ZF1 to ZF2

© 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

© 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

© All rights reserved. Zend Technologies, Inc.

How to contribute

© All rights reserved. Zend Technologies, Inc.

We want you!

● How to contribute:

▶ Write code▶ Documentation▶ Testing▶ Feedbacks/comments

https://github.com/zendframework/zf2

© 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

© All rights reserved. Zend Technologies, Inc.

Thank you!

● Vote this talk:

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

▶ enrico@zend.com

top related