A quick start on Zend Framework 2
Upcoming SlideShare
Loading in...5
×
 

A quick start on Zend Framework 2

on

  • 21,904 views

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

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.

Statistics

Views

Total Views
21,904
Slideshare-icon Views on SlideShare
21,834
Embed Views
70

Actions

Likes
11
Downloads
392
Comments
3

6 Embeds 70

https://twitter.com 63
http://pinterest.com 3
https://si0.twimg.com 1
http://us-w1.rockmelt.com 1
http://tweetedtimes.com 1
http://www.pinterest.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

13 of 3 Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Have you tried packtpubs book about zend framework 2 application development

    http://www.packtpub.com/zend-framework-2-application-development/book
    Are you sure you want to
    Your message goes here
    Processing…
  • sure, we need a book about the class, and their attributes and functions
    Are you sure you want to
    Your message goes here
    Processing…
  • I wan't to use another framework, I need the zf2 but not more documentation about the class, the scructure, about the parameters, we need a book ...;(.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    A quick start on Zend Framework 2 A quick start on Zend Framework 2 Presentation Transcript

    • A quick start onZend Framework 2by Enrico Zimuel (enrico@zend.com)Senior Software EngineerZend Framework Core TeamZend Technologies Ltd 19th May 2012 Verona (Italy) © All rights reserved. Zend Technologies, Inc.
    • About me • 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 © 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 (ZendDi) – Event driven (ZendEventManager) – Standard classes (ZendStdlib)● 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-Stylerequire_once Zend/Loader/StandardAutoloader.php;$loader = new ZendLoaderStandardAutoloader(array( fallback_autoloader => true,));$loader->register(); © All rights reserved. Zend Technologies, Inc.
    • ZF2 NS/Prefixrequire_once Zend/Loader/StandardAutoloader.php;$loader = new ZendLoaderStandardAutoloader();$loader->registerNamespace( My, __DIR__ . /../library/My) ->registerPrefix( Foo_, __DIR__ . /../library/Foo);$loader->register(); © All rights reserved. Zend Technologies, Inc.
    • ZF2 Class-Mapreturn array( MyFooBar => __DIR__ . /Foo/Bar.php,); .classmap.phprequire_once Zend/Loader/ClassMapAutoloader.php;$loader = new ZendLoaderClassMapAutoloader();$loader->registerAutoloadMap( __DIR__ . /../library/.classmap.php);$loader->register(); © 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% improvementNote: 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.
    • ZendDi ● 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 ZendDiDi, ZendDiConfiguration; $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 ZendDiDefinitionAnnotation as Di; class Foo { protected $bar; /** * @DiInject() */ public function setBar(Bar $bar){ $this->bar = $bar; } } class Bar { ... } © All rights reserved. Zend Technologies, Inc.
    • Di by annotation (2)$compiler = new ZendDiDefinitionCompilerDefinition();$compiler->addDirectory(File path of Foo and Bar);$compiler->compile();$definitions = new ZendDiDefinitionList($compiler);$di = new ZendDiDi($definitions);$baz = $di->get(Foo); // contains Bar!More use cases of ZendDi: 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.
    • Exampleuse ZendEventManagerEventManager;$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.
    • Module definition “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) © All rights reserved. Zend Technologies, Inc.
    • Module for ZF2● The basic unit in a ZF2 application is 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.
    • Folders 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 module Application Name of the module config module.config.php src Application Controller IndexController.php view index index.phtml Module.php autoload_classmap.php autoload_functions.php autoload_registers.php © 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.
    • .htaccessRewriteEngine 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.phpchdir(dirname(__DIR__));require_once (getenv(ZF2_PATH) ?: vendor/ZF/library) ./Zend/Loader/AutoloaderFactory.php;use ZendLoaderAutoloaderFactory, ZendServiceManagerServiceManager, ZendMvcServiceServiceManagerConfiguration;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.
    • 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, ), ), service_manager => array( use_defaults => true, factories => array(), ), ); © All rights reserved. Zend Technologies, Inc.
    • Module.phpnamespace Application;class Module{ public function getAutoloaderConfig() { return array( ZendLoaderClassMapAutoloader => array( __DIR__ . /autoload_classmap.php, ), ZendLoaderStandardAutoloader => 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.phpreturn array( ApplicationControllerIndexController => __DIR__ . /src/Application/Controller/IndexController.php, ApplicationModule => __DIR__ . /Module.php,); © All rights reserved. Zend Technologies, Inc.
    • module.config.phpreturn array( router => array( routes => array( default => array( type => ZendMvcRouterHttpSegment, 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 => ZendMvcRouterHttpLiteral, options => array( route => /, defaults => array( controller => IndexController, action => index, ),),),),), © All rights reserved. Zend Technologies, Inc.
    • module.config.php (2) controller => array( classes => array( IndexController => ApplicationControllerIndexController ), ), 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.phpnamespace ApplicationController;use ZendMvcControllerActionController, ZendViewModelViewModel;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 => ZendFoo – Exceptions: an Interface for each components, no more Zend_Exception – Autoloading: 3 options (one is ZF1) – MVC: module, event based, dispatchable – DB: new ZendDb – Form: new ZendForm © 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 ZF1s 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 2 by 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 © All rights reserved. Zend Technologies, Inc.