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

Quick start on Zend Framework 2

Embed Size (px)

DESCRIPTION

Talk presented at the Dutch PHP Conference 2012 in Amsterdam. Hands on the Zend Skeleton Application of ZF 2.0.0beta4

Citation preview

Page 1: 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

Page 2: 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 Technologies in

the Zend Framework Team

• International speaker on PHP and

computer security topics

• Researcher programmer at

Informatics Institute of University

of Amsterdam

• Co-founder of the PUG Torino (Italy)

About me

@ezimuel

[email protected]

www.zimuel.it

Page 3: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

ZF2 in a slide

● New architecture

▶ MVC, Di, Events, Service, Module● Better performance

● Requirement: PHP 5.3.3

● No more CLA (Contributor License Agreement)

● Git (GitHub) instead of SVN

● Packaging system

▶ pyrus, composer

Page 4: 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: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

MVC is event driven

● Everything is an event

dispatchbootstrap route

Listeners

Page 6: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Releases

● ZF2.0.0beta4

● Goal:

▶ beta5 at the end of June▶ ZF 2.0 RC this summer!!!

Page 7: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Quick startZend Skeleton Application

Page 8: 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

● How to install:$ cd my/project/dir$ git clone git://github.com/zendframework/ZendSkeletonApplication.git$ cd ZendSkeletonApplication$ php composer.phar install

Page 9: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Output

Page 10: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Folder's tree

config

data

module

public

vendor

Page 11: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Config folder

config

autoload

application.config.php

data

module

public

vendor

Page 12: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Data folder

config

data

cache

module

public

vendor

Page 13: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Module folder

moduleApplication

configmodule.config.php

srcApplication

ControllerIndexController.php

viewapplication

indexindex.phtml

errorlayout

Module.php

Name of the module

Page 14: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Public folder

public

images

js

css

.htaccess

index.php

Page 15: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Vendor folder

config

data

module

public

vendor

zendframework

Page 16: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

configuration

Page 17: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

/config/application.config.php

Page 18: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

public folder

Page 19: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

public/.htaccess

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

Page 20: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

public/index.php

Page 21: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Zend\ServiceManager

● The ServiceManager is a Service Locator implementation

● A Service Locator is a well-known object in which you may register objects (more in general services) and later retrieve them

● Driven by configuration

Page 22: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Types of Services

● Explicit (name => object pairs)

● Invokables (name => class to instantiate)

● Factories (name => callable returning object)

● Aliases (name => some other name)

● Abstract Factories (unknown services)

● Scoped Containers (limit what can be created)

● Shared (or not; you decide)

Page 23: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

module

Page 24: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

A module is all related code and assets that solve a specific problem.

Modules inform the MVC about services and event listeners

Modules by default

Page 25: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Modules 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 26: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Develop Modules

● Modules contain all logic related to a discrete application problem.

▶ Controllers▶ Entities▶ Plugins▶ Etc.

● 99% of the time, you will write modules

Page 27: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

\module\Application\Module.php

Page 28: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

/module/Application/config/module.config.php(routing part)

Page 29: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

/module/Application/config/module.config.php(controller part)

Page 30: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

/module/Application/config/module.config.php(view)

Page 31: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

/module/Application/src/Application/Controller/IndexController.php

Passing array() of variables to the view

Page 32: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Packaging system

Page 33: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Pyrus

● 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 34: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Composer

● New package management and distribution

tool

● http://packagist.org/

● http://getcomposer.org/

Page 35: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

composer.json

Page 36: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

From ZF1 to ZF2

Page 37: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

From ZF1 to ZF2

● Goal: migrate without rewriting much code!

● Main steps

▶ Namespace: Zend_Foo => Zend\Foo▶ Autoloading: 3 options ▶ MVC: module, event based, dispatchable▶ DB: new Zend\Db▶ Form: new Zend\Form

● Can run in parallel (instant migration!)

Page 38: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Conclusion:ZF2 rocks!

Page 39: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Why ZF2 rocks!

● Based on solid architecture principles

● Very fast (focused on optimization)

● Open architecture (event driven)

● Support of modules (reuse of the code)

● Driven by the community (100% open source)

● Completed tested (PHPUnit, Travis CI)

● Packages (pyrus, composer)

Page 40: 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 41: 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 42: Quick start on Zend Framework 2

© All rights reserved. Zend Technologies, Inc.

Thank you!

● Comment this talk at:

▶ https://joind.in/6237● Direct contact:

[email protected]▶ @ezimuel