You are on page 1of 52

All rights reserved. Zend Technologies, Inc.

A quick start on
Zend Framework 2
by Enrico Zimuel (enrico@zend.com)
Senior Software Engineer
Zend Framework Core Team
Zend Technologies Ltd
!
th
"ay #$# %erona (&taly)
All rights reserved. Zend Technologies, Inc.
Enrico Zimuel (@ezimuel)
'o(tware Engineer since !!)
*ssembly +,)- C.C//- 0a1a- 2erl- 232
232 Engineer at Zend in the Zend
Framework Core Team
&nternational s4eaker about 232 and
com4uter security to4ics
Co5author o( the italian book
6232 7est 4ractices8 (F*9 edizioni)
Co5(ounder o( the 2:9 Torino
About me
All rights reserved. Zend Technologies, Inc.
ZF2 in a slide

;ew architecture ("%C- <i- E1ents)

=e>uirement? PHP 5.3.3

;o more CLA (Contributor License *greement)

Git (9it3ub) instead o( '%;

7etter 4er(ormance (new autoload)

"odule su44ort

2ackaging system (4yrus)


All rights reserved. Zend Technologies, Inc.
A new core

The ZF1 way?

'ingleton- =egistry- and 3ard5


Coded <e4endencies

The ZF2 a44roach?

*s4ect @riented <esign and


<e4endency &nAection
All rights reserved. Zend Technologies, Inc.
ew arc!itectural a""roac!

"ethodologies used in the de1elo4ment


<ecou4ling (ZendB<i)
E1ent dri1en (ZendBE1ent"anager)
'tandard classes (ZendB'tdlib)

Take ad1antage o( 232 C.D

;ames4ace

Lambda Functions and Closures

7etter 4er(ormance
All rights reserved. Zend Technologies, Inc.
#eleases

ZF2.$.$beta% ne&t week'

9oal?

betaC on 0une

ZF #.$ =C this summerEEE


All rights reserved. Zend Technologies, Inc.
Autoloadin(
All rights reserved. Zend Technologies, Inc.
Autoloadin(

;o more require_once callsE

"ulti4le a44roaches?
ZF5style includeF4ath autoloader
2er5names4ace.4re(i+ autoloading
Class5ma4 autoloading
All rights reserved. Zend Technologies, Inc.
ZF1)*t+le
require_once 'Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array(
'fallac!_autoloader' =" true#
$$;
$loader%"re&i'ter($;
All rights reserved. Zend Technologies, Inc.
ZF2 *,Pre-i&
require_once 'Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader($;
$loader%"re&i'ter(a)e'pace(
'*y'# __+,-__ . '/../lirary/*y'$
%"re&i'ter.refi/(
'0oo_'# __+,-__ . '/../lirary/0oo'$;
$loader%"re&i'ter($;
All rights reserved. Zend Technologies, Inc.
ZF2 Class).a"
return array(
'*y\0oo\1ar' =" __+,-__ . '/0oo/1ar.php'#
$;
require_once 'Zend/Loader/2la''*apAutoloader.php';
$loader = new Zend\Loader\2la''*apAutoloader($;
$loader%"re&i'terAutoload*ap(
__+,-__ . '/../lirary/.cla'')ap.php'$;
$loader%"re&i'ter($;
.cla'')ap.php
All rights reserved. Zend Technologies, Inc.
Classma" (enerator

3ow to generate the .classma4.4h4G


He 4ro1ided a command line tool?
bin,classma"/(enerator."!"

:sage is tri1ial?
$ cd your/lirary
$ php /path/to/cla'')ap_&enerator.php %w

Class5"a4 will be created in .classma"."!"


All rights reserved. Zend Technologies, Inc.
Per-ormance im"ro0ement

Com"ared wit! t!e ZF1 autoloader

Class).a"s
show a #C5,CI im4ro1ement

ames"aces,"re-i&es
shows $5J$I im4ro1ement
ote1 2!e new autoloadin( s+stem o- ZF2 !as been "orted to ZF 1.12
All rights reserved. Zend Technologies, Inc.
3e"endenc+
4n5ection
All rights reserved. Zend Technologies, Inc.
Zend63i

'u44orts the D di((erent inAection 4atterns?


Constructor
&nter(ace
'etter

&m4lements a <i Container?


"anage the de4endencies using con-i(uration and
annotation
2ro1ide a com"iler to autodisco1er classes in a 4ath
and create the class de(initions- (or de4endencies
All rights reserved. Zend Technologies, Inc.
7&am"le
cla'' 1ar
3 4 5
cla'' 0oo
3
protected $ar;
4
pulic function 'et1ar(1ar $ar$
3
$thi'%"ar = $ar;
5
4
5
All rights reserved. Zend Technologies, Inc.
*am"le de-inition
$definition = array(
'0oo' =" array(
''et1ar' =" array(
'ar' =" array(
'type' =" '1ar'#
'required' =" true#
$#
$#
$#
$;
All rights reserved. Zend Technologies, Inc.
8sin( t!e 3i container
u'e Zend\+i\+i#
Zend\+i\2onfi&uration;

$di = new +i;
$confi& = new 2onfi&uration(array(
'definition' =" array('cla''' =" $definition$
$$;
$confi&%"confi&ure($di$;

$foo = $di%"&et('0oo'$; // contain' 1ar6
All rights reserved. Zend Technologies, Inc.
3i b+ annotation
u'e Zend\+i\+efinition\Annotation a' +i;

cla'' 0oo 3
protected $ar;
/77
7 8+i\,n9ect($
7/
pulic function 'et1ar(1ar $ar$3
$thi'%"ar = $ar;
5
5

cla'' 1ar 3 ... 5
All rights reserved. Zend Technologies, Inc.
3i b+ annotation 92:
$co)piler = new Zend\+i\+efinition\2o)piler+efinition($;
$co)piler%"add+irectory('0ile path of 0oo and 1ar'$;
$co)piler%"co)pile($;
$definition' = new Zend\+i\+efinitionLi't($co)piler$;
$di = new Zend\+i\+i($definition'$;
$a: = $di%"&et('0oo'$; // contain' 1ar6
"ore use cases o( ZendB<i?
htt4s?..github.com.ral4hschindler.z(#5di5use5cases
All rights reserved. Zend Technologies, Inc.
70ent .ana(er
All rights reserved. Zend Technologies, Inc.
70ent .ana(er

*n 70ent .ana(er is an obAect that aggregates


listeners (or one or more named e1ents- and
which triggers e1ents.

* Listener is a callback that can react to an


e1ent.

*n 70ent is an action.
All rights reserved. Zend Technologies, Inc.
7&am"le
u'e Zend\;<ent*ana&er\;<ent*ana&er;
$e<ent' = new ;<ent*ana&er($;
$e<ent'%"attach('do'# function($e$ 3
$e<ent = $e%"&et(a)e($;
$para)' = $e%"&et.ara)'($;
printf(
'=andled e<ent >?'@# with para)eter' ?''#
$e<ent#
9'on_encode($para)'$
$;
5$;
$para)' = array('foo' =" 'ar'# 'a:' =" 'at'$;
$e<ent'%"tri&&er('do'# null# $para)'$;
All rights reserved. Zend Technologies, Inc.
.;C
All rights reserved. Zend Technologies, Inc.
70ent dri0en arc!itecture

E1erything is an e0ent in the "%C architecture o( ZF#


All rights reserved. Zend Technologies, Inc.
6* module is a collection o( code and other
(iles that sol1es a more s4eci(ic atomic
4roblem o( the larger business 4roblem.8
((rom the ZF# =FC)
.odule de-inition
All rights reserved. Zend Technologies, Inc.
.odule -or ZF2

The basic unit in a ZF# a44lication


is a .odule

"odules are <Plu( and "la+= technology

"odules are sim4le?

* names4ace

Containing a single class(ile? .odule."!"


All rights reserved. Zend Technologies, Inc.
>uick start
Zend *keleton A""lication
All rights reserved. Zend Technologies, Inc.
Zend *keleton A""lication

* sim4le- skeleton a44lication using the new "%C


layer and the module system

9ithub?

git clone 55recursi1e


git?..github.com.zend(ramework.Zend'keleton*44lication.git

This 4roAect makes use o( 9it submodules

=eady (or ZF2.$.$beta%


All rights reserved. Zend Technologies, Inc.
FolderKs tree
con(ig
data
module
4ublic
1endor
All rights reserved. Zend Technologies, Inc.
Con(ig (older
con-i(
autoload
a44lication.con(ig.4h4
data
module
4ublic
1endor
All rights reserved. Zend Technologies, Inc.
<ata (older
con(ig
data
cache
module
4ublic
1endor
All rights reserved. Zend Technologies, Inc.
"odule (older
module
*44lication
con(ig
module.con(ig.4h4
src
*44lication
Controller
&nde+Controller.4h4
1iew
inde+
inde+.4html
.odule."!"
autoloadFclassma4.4h4
autoloadF(unctions.4h4
autoloadFregisters.4h4
;ame o( the module
All rights reserved. Zend Technologies, Inc.
2ublic (older
"ublic
images
As
css
.htaccess
inde+.4h4
All rights reserved. Zend Technologies, Inc.
%endor (older
con(ig
data
module
4ublic
0endor
ZendFramework
All rights reserved. Zend Technologies, Inc.
.htaccess
RewriteEngine On
RewriteCond %{REQUEST!I"E#A$E% &s 'OR(
RewriteCond %{REQUEST!I"E#A$E% &l 'OR(
RewriteCond %{REQUEST!I"E#A$E% &d
RewriteR)le *.+, & '#C,"(
RewriteR)le *.+, inde-..h. '#C,"(
All rights reserved. Zend Technologies, Inc.
inde+.4h4
chdir/dirn01e/2IR334
re5)ireonce /getenv/6Z!78AT963 :; 6vendor<Z!<li=r0r>63 .
6<Zend<"o0der<A)tolo0der!0ctor>..h.64
)se Zend?"o0der?A)tolo0der!0ctor>,
Zend?Service$0n0ger?Service$0n0ger,
Zend?$vc?Service?Service$0n0gerCon@ig)r0tion4
A)tolo0der!0ctor>;;@0ctor>/34
$confi& A include 'confi&/application.confi&.php'4
$'er<ice*ana&er A new Service$0n0ger/new Service$0n0gerCon@ig)r0tion/
$confi&'''er<ice_)ana&er'(334
$'er<ice*ana&er&BsetService/'Application2onfi&uration', $confi&34
$'er<ice*ana&er&Bget/'*odule*ana&er'3&Blo0d$od)les/34
$'er<ice*ana&er&Bget/'Application'3&B=ootstr0./3&Br)n/3&Bsend/34
All rights reserved. Zend Technologies, Inc.
return array(
KmodulesK LM array(
K*44licationK-
)-
KmoduleFlistenerFo4tionsK LM array(
Kcon(igFcacheFenabledK LM (alse-
KcacheFdirK LM Kdata.cacheK-
KmoduleF4athsK LM array(
K..moduleK-
K..1endorK-
)-
)-
Kser1iceFmanagerK LM array(
KuseFde(aultsK LM true-
K(actoriesK LM array()-
)-
)N
a44lication.con(ig.4h4
All rights reserved. Zend Technologies, Inc.
"odule.4h4
names"ace *44licationN
class .odule
O
"ublic -unction (etAutoloaderCon-i(()
O
return arra+(
KZendBLoaderBClass"a4*utoloaderK LM arra+(
FF<&=FF . K.autoloadFclassma4.4h4K-
)-
KZendBLoaderB'tandard*utoloaderK LM arra+(
Knames4acesK LM arra+(
FF;*"E'2*CEFF LM FF<&=FF . K.src.K . FF;*"E'2*CEFF-
)-
)-
)N
P
"ublic -unction (etCon-i(()
O
return incl)de FF<&=FF . K.con(ig.module.con(ig.4h4KN
P
P
All rights reserved. Zend Technologies, Inc.
autoloadFclassma4.4h4
return arra+(
K*44licationBControllerB&nde+ControllerK LM
FF<&=FF . K.src.*44lication.Controller.&nde+Controller.4h4K-
K*44licationB"oduleK LM
FF<&=FF . K."odule.4h4K-
)N
All rights reserved. Zend Technologies, Inc.
module.con(ig.4h4
return array(
KrouterK LM array(
KroutesK LM array(
Kde(aultK LM array(
Kty4eK LM KZendB"1cB=outerB3tt4B'egmentK-
Ko4tionsK LM array(
KrouteK LM K.Q?controllerQ.?actionRRK-
KconstraintsK LM array(
KcontrollerK LM KQa5z*5ZRQa5z*5Z$5!F5RSK-
KactionK LM KQa5z*5ZRQa5z*5Z$5!F5RSK-
)-
Kde(aultsK LM array(
KcontrollerK LM K&nde+ControllerK-
KactionK LM Kinde+K-
)-)-)-
KhomeK LM array(
Kty4eK LM KZendB"1cB=outerB3tt4BLiteralK-
Ko4tionsK LM array(
KrouteK LM K.K-
Kde(aultsK LM array(
KcontrollerK LM K&nde+ControllerK-
KactionK LM Kinde+K-
)-)-)-)-)-
All rights reserved. Zend Technologies, Inc.
module.con(ig.4h4 (#)
KcontrollerK LM array(
KclassesK LM array(
K&nde+ControllerK LM K*44licationBControllerB&nde+ControllerK
)-
)-
K1iewFmanagerK LM array(
Kdis4layFnotF(oundFreasonK LM true-
Kdis4layFe+ce4tionsK LM true-
Kdocty4eK LM K3T"LCK-
KnotF(oundFtem4lateK LM Kerror.J$JK-
Ke+ce4tionFtem4lateK LM Kerror.inde+K-
Ktem4lateFma4K LM array(
Klayout.layoutK LM FF<&=FF . K....1iew.layout.layout.4htmlK-
Kinde+.inde+K LM FF<&=FF . K....1iew.inde+.inde+.4htmlK-
Kerror.J$JK LM FF<&=FF . K....1iew.error.J$J.4htmlK-
Kerror.inde+K LM FF<&=FF . K....1iew.error.inde+.4htmlK-
)-
Ktem4lateF4athFstackK LM array(
Ka44licationK LM FF<&=FF . K....1iewK-
)-
)-
)N
All rights reserved. Zend Technologies, Inc.
&nde+Controller.4h4
names4ace *44licationBControllerN
use ZendB"1cBControllerB*ctionController-
ZendB%iewB"odelB%iew"odelN
class &nde+Controller e+tends *ctionController
O
4ublic (unction inde+*ction()
O
return new %iew"odel()N
P
P
All rights reserved. Zend Technologies, Inc.
Packa(in( s+stem
All rights reserved. Zend Technologies, Inc.
*ource "acka(e

htt4?..4ackages.zend(ramework.com.

<ownload or use "+rus- a 2E*=# installer

2yrus 4ackages?

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.
.i(rate to ZF2

9oal? migrate without rewriting much codeE

"ain ste4s
ames"ace? ZendFFoo LM ZendBFoo
7&ce"tions? an &nter(ace (or each com4onents-
no more ZendFE+ce4tion
Autoloadin(? D o4tions (one is ZF)
.;C? module- e1ent based- dis4atchable
3?1 new ZendB<b
Form1 new ZendBForm
All rights reserved. Zend Technologies, Inc.
ZF1 mi(ration "rotot+"e

'ource code? htt4?..bit.ly.41c$T

Creates a UZ(Com4atU 1ersion o( the ZF dis4atcher


as an e1ent listener.

The bootstra4 largely mimics how ZFKs


ZendF*44lication bootstra4 works.

The de(ault route utilizes the new ZF# "%C routing-


but mimics what ZF 4ro1ided
All rights reserved. Zend Technologies, Inc.
How to contribute
All rights reserved. Zend Technologies, Inc.
@e want +ou'

3ow to contribute?

@rite code

3ocumentation

2estin(

Feedbacks,comments
htt4s?..github.com.zend(ramework.z(#
All rights reserved. Zend Technologies, Inc.
Hel"in( out

htt4?..(ramework.zend.com.z(#

htt4?..github.com.zend(ramework

htt4s?..github.com.zend(ramework.Zend'keleton*44lication

9etting 'tarted with Zend Framework #


by =ob *llen- htt4?..www.akrabat.com

Heekly &=C meetings (Vz(#5meeting on Freenode)

Vz(talk.# on Freenode &=C


All rights reserved. Zend Technologies, Inc.
2!ank +ou'

;ote t!is talk1

!tt"s1,,5oind.in,A3B%

Comments and -eedbacks1

enricoCDend.com

You might also like