Вы находитесь на странице: 1из 37

The MVC architecture of ZF2

by Enrico Zimuel (enrico@zend.com) Senior Software Engineer Zend Framework Core Team Zend Technologies Ltd

All rights reserved. Zend Technologies, Inc.

About me
Enrico Zimuel o!tware Engineer since "##$
%ssembly &'$( C)C**( +a,a( -erl( -.-

-.- Engineer at Zend Technologies in the


@ezimuel enrico@zend.com www.zimuel.it

Zend Framework Team /nternational s0eaker on -.- and com0uter security to0ics 1esearcher 0rogrammer at /n!ormatics /nstitute o! 2ni,ersity o! %msterdam Co3!ounder o! -24 Torino (/taly)

All rights reserved. Zend Technologies, Inc.

ZF2 in a slide

5ew architecture

67C( 8i( E,ents( er,ice( 6odule

-er!ormance im0ro,ement (lazy loading) 1e9uirement: PHP 5.3.3 - 13; com0liant -ackaging system (0yrus( com0oser) ZF ;.<.= last stable

htt0:))!ramework.zend.com

All rights reserved. Zend Technologies, Inc.

A new core

The ZF way:

ingleton( 1egistry( and .ard3 Coded 8e0endencies %s0ect >riented 8esign and 8e0endency /n?ection

The ZF2 a00roach:

All rights reserved. Zend Technologies, Inc.

Model View Controller

All rights reserved. Zend Technologies, Inc.

MVC ! Model" View" Controller

All rights reserved. Zend Technologies, Inc.

MVC

The central idea behind 67C is code reusability and se0aration o! concerns

All rights reserved. Zend Technologies, Inc.

MVC architecture of ZF2


E,erything is an e#ent

.TTre9uest

bootstra$

route

dis$atch

.TTres0onse

Listeners

All rights reserved. Zend Technologies, Inc.

A common wor%flow

use Zend@ er,ice6anager@ er,ice6anagerA use Zend@6,c@%00licationA )B C B) Dser,ices E new er,ice6anager(Dser,icesCon!ig)A Da00 E new %00lication(Da00Con!ig( Dser,ices)A Da003Fbootstra0()A Dres0onse E Da003Frun()A Dres0onse3Fsend()A

All rights reserved. Zend Technologies, Inc.

&efault ser#ices

%00lication e&0ects a er,ice6anager( con!igured with the !ollowing ser,ices:

E,ent6anager 6odule6anager 1e9uest 1es0onse 1outeListener 1outer 8is0atchListener 7iew6anager


All rights reserved. Zend Technologies, Inc.

ZF2
'%eleton A$$lication

All rights reserved. Zend Technologies, Inc.

ZF2 s%eleton a$$lication


htt0s:))github.com)zend!ramework)Zend keleton%00lication

/nstall using com0oser:


curl 3s htt0s:))getcom0oser.org)installer G 0h0 33 0h0 com0oser.0har create30ro?ect 33re0ository3 urlEHhtt0:))0ackages.zend!ramework.comH zend!ramework)skeleton3a00lication 0ath)to)install

All rights reserved. Zend Technologies, Inc.

com$oser.(son
I HnameH: Hzend!ramework)skeleton3a00licationH( Hdescri0tionH: H keleton %00lication !or ZF;H( HlicenseH: HJ 83=3ClauseH( HkeywordsH: K H!rameworkH( Hz!;H L( Hhome0ageH: Hhtt0:))!ramework.zend.com)H( Hre9uireH: I H0h0H: HFEM.=.=H( Hzend!ramework)zend!rameworkH: H;.BH N

All rights reserved. Zend Technologies, Inc.

8irectory tree
con!ig data module 0ublic ,endor

All rights reserved. Zend Technologies, Inc.

Con!ig !older
confi) autoload a00lication.con!ig.0h0 data module 0ublic ,endor

All rights reserved. Zend Technologies, Inc.

8ata !older
con!ig data cache module 0ublic ,endor

All rights reserved. Zend Technologies, Inc.

6odule !older
module
5ame o! the module %00lication con!ig module.con!ig.0h0 src %00lication Controller /nde&Controller.0h0 ,iew a00lication inde& inde&.0html error layout Module.$h$

All rights reserved. Zend Technologies, Inc.

-ublic !older
$ublic images ?s css .htaccess inde&.0h0

All rights reserved. Zend Technologies, Inc.

7endor !older
con!ig data module 0ublic #endor zend!ramework

All rights reserved. Zend Technologies, Inc.

confi)uration

All rights reserved. Zend Technologies, Inc.

*confi)*a$$lication.confi).$h$
return array( OmodulesO EF array( O%00licationO( )( OmodulePlistenerPo0tionsO EF array( Ocon!igPglobP0athsO EF array( Ocon!ig)autoload)I(B.NIglobal(localN.0h0O( )( OmoduleP0athsO EF array( O.)moduleO( O.),endorO( )( )( )A
All rights reserved. Zend Technologies, Inc.

$ublic folder

All rights reserved. Zend Technologies, Inc.

$ublic*.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.

Front controller +$ublic*inde,.$h$QR0h0 )BB B This makes our li!e easier when dealing with 0aths. E,erything is relati,e B to the a00lication root now. B) chdir(dirname(PP8/1PP))A )) etu0 autoloading include OinitPautoloader.0h0OA )) 1un the a00licationS Zend@6,c@%00lication::init(include Ocon!ig)a00lication.con!ig.0h0O)3Frun()3Fsend()A

All rights reserved. Zend Technologies, Inc.

Zend.'er#iceMana)er

The er,ice6anager is a 'er#ice /ocator im0lementation % er,ice Locator is a well3known ob?ect in which you may re)ister ob(ects (more in general ser,ices) and later retrie#e them 8ri,en by con!iguration

All rights reserved. Zend Technologies, Inc.

T0$es of 'er#ices

E&0licit (name EF ob?ect 0airs) /n,okables (name EF class to instantiate) Factories (name EF callable returning ob?ect) %liases (name EF some other name) %bstract Factories (unknown ser,ices) co0ed Containers (limit what can be created) hared (or notA you decide)

All rights reserved. Zend Technologies, Inc.

module

All rights reserved. Zend Technologies, Inc.

Modules b0 default

% module is all related code and assets that solve a specific problem. 6odules inform the MVC about ser,ices and e,ent listeners

All rights reserved. Zend Technologies, Inc.

Modules for ZF2

The basic unit in a ZF; a00lication is a Module 6odules are 1Plu) and $la02 technology 6odules are sim0le:

% names0ace Containing a single class!ile: Module.$h$

All rights reserved. Zend Technologies, Inc.

&e#elo$ Modules

6odules contain all logic related to a discrete a00lication 0roblem.


Controllers Entities -lugins Etc.

##T o! the time( you will write modules

All rights reserved. Zend Technologies, Inc.

.module.A$$lication.Module.$h$
names0ace %00licationA use Zend@6,c@6odule1outeListenerA use Zend@6,c@6,cE,entA class 6odule I 0ublic !unction getCon!ig() I return include PP8/1PP . O)con!ig)module.con!ig.0h0OA N N

All rights reserved. Zend Technologies, Inc.

*module*A$$lication*confi)*module.confi).$h$ +routin) $artreturn array( OrouterO EF array( OroutesO EF array( OhomeO EF array( Oty0eO EF OZend@6,c@1outer@.tt0@LiteralO( Oo0tionsO EF array( OrouteO EF O)O( Ode!aultsO EF array( OcontrollerO EF O%00lication@Controller@/nde&O( OactionO EF Oinde&O( )( )( )( ...

All rights reserved. Zend Technologies, Inc.

*module*A$$lication*confi)*module.confi).$h$ +routin) $art 2...


Oa00licationO EF array( Oty0eO EF OLiteralO( Oo0tionsO EF array( OrouteO EF O)a00licationO( Ode!aultsO EF array( OPP5%6E -%CEPPO EF O%00lication@ControllerO( OcontrollerO EF O/nde&O( OactionO EF Oinde&O( )( )( OmayPterminateO EF true( OchildProutesO EF array( Ode!aultO EF array( Oty0eO EF O egmentO( Oo0tionsO EF array( OrouteO EF O)K:controllerK):actionLLO( OconstraintsO EF array( OcontrollerO EF OKa3z%3ZLKa3z%3Z<3#P3LBO( OactionO EF OKa3z%3ZLKa3z%3Z<3#P3LBO( )( Ode!aultsO EF array( )( ...
All rights reserved. Zend Technologies, Inc.

*module*A$$lication*confi)*module.confi).$h$ +controller 3 translator $artC Oser,icePmanagerO EF array( O!actoriesO EF array( OtranslatorO EF OZend@/"'n@Translator@Translator er,iceFactoryO( )( )( OtranslatorO EF array( OlocaleO EF OenP2 O( OtranslationP!ileP0atternsO EF array( array( Oty0eO EF Ogette&tO( ObasePdirO EF PP8/1PP . O)..)languageO( O0atternO EF OTs.moO( )( )( )( OcontrollersO EF array( Oin,okablesO EF array( O%00lication@Controller@/nde&O EF O%00lication@Controller@/nde&ControllerO )( )( ...

All rights reserved. Zend Technologies, Inc.

*module*A$$lication*confi)*module.confi).$h$ +#iew... O,iewPmanagerO EF array( Odis0layPnotP!oundPreasonO EF true( Odis0layPe&ce0tionsO EF true( Odocty0eO EF O.T6LMO( OnotP!oundPtem0lateO EF Oerror)U<UO( Oe&ce0tionPtem0lateO EF Oerror)inde&O( Otem0latePma0O EF array( Olayout)layoutO EF PP8/1PP . O)..),iew)layout)layout.0htmlO( Oa00lication)inde&)inde&O EF PP8/1PP . O)..),iew)a00lication)inde&)inde&.0htmlO( Oerror)U<UO EF PP8/1PP . O)..),iew)error)U<U.0htmlO( Oerror)inde&O EF PP8/1PP . O)..),iew)error)inde&.0htmlO( )( Otem0lateP0athPstackO EF array( PP8/1PP . O)..),iewO( )( )( ...

All rights reserved. Zend Technologies, Inc.

*module*A$$lication*src*A$$lication* Controller*4nde,Controller.$h$

names0ace %00lication@ControllerA use Zend@6,c@Controller@%bstract%ctionControllerA use Zend@7iew@6odel@7iew6odelA class /nde&Controller e&tends %bstract%ctionController I 0ublic !unction inde&%ction() I return new 7iew6odel()A N N

All rights reserved. Zend Technologies, Inc.

Than% 0ou5

More information

htt$6**framewor%.7end.com 97ftal%" 97ftal%.de#

48C channels +freenode

All rights reserved. Zend Technologies, Inc.