Anda di halaman 1dari 15

LearnDooPHP

Login Home About Contact Forum SUBSCRIBETORSS LearnDooPHP:HighperformancePHPframework 29Sep2009

byMilosKovacki 31Comments Demos&Snippets

ConceptofhowtoworkwithDooFramework
OkinthisarticleIwillshowyouhowshouldyouworkwithDooFramework,whenIammakingwebsiteIliketo haveonesuperclassthatextendsDooControllerclassandinconstructorofthatclassIwilladdallstuffneededformy application,soletsmakethatclass,weshouldnameitforexampleCoreController
0 0 1 . 0 0 2 . 0 0 3 . 0 0 4 . 0 0 5 . 0 0 6 . 0 0 7 . 0 0 8 . 0 0 9 . 0 1 0 . 0 1 1 . 0 1 2 . 0 1 3 . 0 1 4 . 0 1 5 . 0 1 6 . 0 1 7 . 0 1 8 . 0 1 9 . 0 2 0 . 0 2 1 . 0 2 2 . 0 2 3 . 0 2 4 . 0 2 5 . 0 2 6 . < ? p h p c l a s s C o r e C o n t r o l l e r e x t e n d s D o o C o n t r o l l e r { / * * * C u r r e n t U R L * / p r o t e c t e d s t a t i c $ _ c u r r e n t U r l = n u l l / * * * I n s t a n c e o f D o o : : d b * / p r o t e c t e d $ _ d b = n u l l / * * * T r a n s l a t o r * / p r o t e c t e d $ _ t r a n s l a t e = n u l l / * * * I n s t a n c e o f D o o : : c a c h e * / p r o t e c t e d $ _ c a c h e = n u l l

0 2 7 . 0 2 8 . 0 2 9 . 0 3 0 . 0 3 1 . 0 3 2 . 0 3 3 . 0 3 4 . 0 3 5 . 0 3 6 . 0 3 7 . 0 3 8 . 0 3 9 . 0 4 0 . 0 4 1 . 0 4 2 . 0 4 3 . 0 4 4 . 0 4 5 . 0 4 6 . 0 4 7 . 0 4 8 . 0 4 9 . 0 5 0 . 0 5 1 . 0 5 2 . 0 5 3 . 0 5 4 . 0 5 5 . 0 5 6 . 0 5 7 . 0 5 8 . 0 5 9 . 0 6 0 . 0 6 1 . 0 6 2 . 0 6 3 . 0 6 4 . 0 6 5 . 0 6 6 . 0 6 7 . 0 6 8 . 0 6 9 . 0 7 0 . 0 7 1 . 0 7 2 . 0 7 3 . 0 7 4 . 0 7 5 . 0 7 6 . 0 7 7 . 0 7 8 . 0 7 9 . 0 8 0 . 0 8 1 . 0 8 2 . 0 8 3 . 0 8 4 . 0 8 5 . 0 8 6 . 0 8 7 . 0 8 8 . 0 8 9 . 0 9 0 . 0 9 1 . 0 9 2 . 0 9 3 . 0 9 4 . 0 9 5 . 0 9 6 . 0 9 7 . 0 9 8 . 0 9 9 . 1 0 0 . 1 0 1 .

/ * * * B a s e p a t h o f a p p l i c a t i o n * / p u b l i c $ _ b a s e P a t h = n u l l / * * * I n s t a c e o f D o o S e s s i o n * / p u b l i c $ _ s e s s i o n = n u l l / * * * I n s t a n c e o f D o o A c l * / p u b l i c $ _ a c l = n u l l / * * * H o s t * / p u b l i c $ h o s t = n u l l / * * * J s p a t h * / p u b l i c $ j s P a t h = n u l l p u b l i c f u n c t i o n _ _ c o n s t r u c t ( ) { $ t h i s > _ b a s e P a t h = D o o : : c o n f ( ) > R O O T _ D I R / / a d d s o m e g l o b a l s t h a t w e n e e d $ t h i s > _ d b = D o o : : d b ( ) $ t h i s > _ v i e w = D o o C o n t r o l l e r : : v i e w ( ) $ t h i s > h o s t = D o o : : c o n f ( ) > h o s t $ t h i s > _ v i e w > h o s t = D o o : : c o n f ( ) > h o s t / / A C L $ t h i s > _ a c l = D o o : : a c l ( ) / / a d d s e s s i o n s $ t h i s > _ s e s s i o n = D o o : : s e s s i o n ( " m y w e b s i t e " ) $ t e m p l a t e V a r i a b l e s = $ t h i s > g e t S e t t i n g s ( ) / / s e s s i o n $ t h i s > _ v i e w > _ s e s s i o n = $ t h i s > _ s e s s i o n $ t h i s > _ c a c h e = D o o : : c a c h e ( ' a p c ' ) $ t h i s > j s P a t h = $ t h i s > _ v i e w > h o s t . ' s t a t i c / j s / ' } / * * * B e f o r e r u n m e t h o d * / p u b l i c f u n c t i o n b e f o r e R u n ( $ r e s o u r c e , $ a c t i o n ) { $ t h i s > _ v i e w > r e q u e s t I n f o = a r r a y ( " c o n t r o l l e r " = > $ r e s o u r c e , " a c t i o n " = > $ a c t i o n ) } / * * * R e d i r e c t m e t h o d * / p u b l i c f u n c t i o n _ r e d i r e c t ( $ u r l ) { h e a d e r ( " L o c a t i o n : " . $ u r l ) } / * * * i s P o s t R e t u r n s t r u e i f m e t h o d i s p o s t * * @ r e t u r n b o o l e a n * / p u b l i c f u n c t i o n i s P o s t ( ) { i f ( $ _ S E R V E R [ ' R E Q U E S T _ M E T H O D ' ] = = " P O S T " ) r e t u r n t r u e e l s e r e t u r n f a l s e }

1 0 2 . 1 0 3 . 1 0 4 . 1 0 5 . 1 0 6 . 1 0 7 . 1 0 8 . 1 0 9 . 1 1 0 . 1 1 1 . 1 1 2 . 1 1 3 . 1 1 4 . 1 1 5 . 1 1 6 . 1 1 7 . 1 1 8 . 1 1 9 . 1 2 0 . 1 2 1 . 1 2 2 . 1 2 3 . 1 2 4 . 1 2 5 . 1 2 6 . 1 2 7 . 1 2 8 . 1 2 9 . 1 3 0 . 1 3 1 . 1 3 2 . 1 3 3 . 1 3 4 . 1 3 5 . 1 3 6 . 1 3 7 . 1 3 8 . 1 3 9 . 1 4 0 . 1 4 1 . 1 4 2 . 1 4 3 . 1 4 4 . 1 4 5 . 1 4 6 . 1 4 7 .

/ * * * i s G e t R e t u r n s t r u e i f m e t h o d i s g e t * * @ r e t u r n b o o l e a n * / p u b l i c f u n c t i o n i s G e t ( ) { i f ( $ _ S E R V E R [ ' R E Q U E S T _ M E T H O D ' ] = = " G E T " ) r e t u r n t r u e e l s e r e t u r n f a l s e } / * * * A p p e n d s f i l e t o h e a d e r * * @ p a r a m a r r a y $ d a t a D a t a f o r v i e w p a r t * @ p a r a m s t r i n g $ u r l U r l o f t h e f i l e * @ p a r a m s t r i n g $ t y p e T y p e o f t h e f i l e e x a m p l e : " t e x t / j a v a s c r i p t " * / p u b l i c f u n c t i o n a p p e n d F i l e ( & $ d a t a , $ u r l , $ t y p e ) { s w i t c h ( $ t y p e ) { c a s e ' t e x t / j a v a s c r i p t ' : $ h t m l = ' < s c r i p t t y p e = " ' . $ t y p e . ' " s r c = " ' . $ u r l . ' " > < / s c r i p t > ' b r e a k c a s e ' ' : b r e a k } i f ( i s s e t ( $ d a t a [ ' s c r i p t s ' ] ) ) { $ d a t a [ ' s c r i p t s ' ] . = $ h t m l } e l s e { $ d a t a [ ' s c r i p t s ' ] = $ h t m l } } / * * * G e t s p a t h t o j a v a s c r i p t f o l d e r * * @ r e t u r n s t r i n g J a v a s c r i p t p a t h * / p u b l i c f u n c t i o n g e t J s P a t h ( ) { r e t u r n $ t h i s > j s P a t h } } ? >

AsyoucanseeIhavesomefunctionsthatIusualyuseineverydevelopment,nowourMainControllerwillextendour CoreControllerclass,ourCoreControllerislocatedinOUR_APP/protected/controllerfolder,andonemorethingin index.phpwemustloadCoreControllerclasswith:


1 . D o o : : l o a d C o n t r o l l e r ( ' C o r e C o n t r o l l e r ' )

WhenweloadourCoreController,wecanmakeourMainControllerclassforexample: Addroutefirst:
1 . $ r o u t e [ ' * ' ] [ ' / ' ] = a r r a y ( ' M a i n C o n t r o l l e r ' , ' i n d e x ' ) 0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . < ? p h p / * * * D e s c r i p t i o n o f M a i n C o n t r o l l e r * * @ a u t h o r M i l o s K o v a c k i * / c l a s s M a i n C o n t r o l l e r e x t e n d s C o r e C o n t r o l l e r { p u b l i c f u n c t i o n i n d e x ( ) { $ t h i s > _ s e s s i o n > t i m e = d a t e ( " Y m d H : i : s " )

1 2 . $ t h i s > _ v i e w > u s e r n a m e = " J o h n S m i t h " 1 3 . $ d a t a [ ' c o n t e n t ' ] = ' i n d e x ' 1 4 . $ t h i s > _ v i e w > r e n d e r ( ' t e m p l a t e ' , $ d a t a , t r u e ) 1 5 . } 1 6 . 1 7 . } 1 8 . ? >

Asyoucanseewewillrendertemplateand$datavariablethathascontentinit,thatcontentisourscriptthatwillbe rendered.Sothisisonewaytousetemplate. Nowletsmaketemplate.phpthatislocatedinOUR_APP/protected/viewcfolder:


1 . 2 . 3 . 4 . 5 . < ? p h p i n c l u d e " h e a d e r . p h p " i n c l u d e " { $ d a t a [ ' c o n t e n t ' ] } . p h p " i n c l u d e " f o o t e r . p h p " ? >

Soasyoucanseeinourtemplatescriptwewillincludeheader/footerandourcontent.Nowwemustmakeindex.php thatwillbeourcontentinMainControllerforindexaction:
1 . 2 . 3 . 4 . < ? p h p e c h o " W e l c o m e " . $ t h i s > u s e r n a m e . " a t " . $ t h i s > _ s e s s i o n > t i m e . ' t i m e ' ? >

Soitsverysimplewedefined2variablesinourcontrollerfunction,oneisstoredinsessionandthatistimeandotheris usernamethatweassignedinouraction.Soasyoucanseeitsprettysimple,youhaveyourskeletonformaking webapps OnemorethingIwanttoshowyouasyoucanseeIhaveonefunctionthatisnamedappendFileIamusingitfor addingsomejsfromcontrollerforsomeactionwhereIneedsomejsscript,forexampleifyouneedsomejsjuston indexactionofMainControlleryoucanaddittroughtcontrolleraction.Iwillnowshowyouhowisthatdoable,we needtoaddlittlesomethingtoourheader.phpwherewehave<head>tag,soinour<head>tagweadd:


1 . < ? p h p i f ( i s s e t ( $ d a t a [ ' s c r i p t s ' ] ) ) e c h o $ d a t a [ ' s c r i p t s ' ] ? >

Nowiftherearesomescritptsindataechothem.NowweusefunctionappendFile:
1 . $ t h i s > a p p e n d F i l e ( $ d a t a , $ t h i s > g e t J s P a t h ( ) . ' m a i n / i n d e x . j s ' , $ t y p e = ' t e x t / j a v a s c r i p t ' )

Wedothisbeforewedorender,butyoualreadyknowthat.Wellthatsaboutit. Tnxforreading,pleaseaskquestions.

31Comments
PreviousPost NextPost

Zares

29Sep,2009

Milos,myattempttostarttheapplicationwithplacingCoreController.phpinadirectoryofcontrollershasfailed! Theerrormessage:DooControllernotfound! ItbegantoworkafterIhavemovedCoreControllerinprotected/class.

Mil0s 29Sep,2009

Hmmthatisverystrange,maybeyoucanprovideuswithcodeofyourindex.php(boostrap),Irealydont seewhatcouldbetheproblem.Butifitsworkingin/classdirectorythenitsokitdoesntrealymatter.

Zares 29Sep,2009
Search...
Random Share JoinChat

Milos,heredetailedresultsofanapplicationtesting. Version(I) 1.index.phpisnotmodified 2.incommon.conf.phpaddedset_include_path($config['SITE_PATH'].protected/class) 3.CoreController.phpindirectoryclas 4.inMainController.phpaddedDoo::loadClass(CoreController) Result:OK! Version(II) 1.inindex.phpaddedDoo::loadController(CoreController) 2.incommon.conf.phpdeletedset_include_path($config['SITE_PATH'].protected/class) 3.CoreController.phpindirectorycontroller 4.inMainController.phpdeletedDoo::loadClass(CoreController) Result:Fatalerror:ClassDooControllernotfoundinD:\\controller\CoreController.phponline3

Mil0s 29Sep,2009 +1

Hmmthatisverystrange,youjustneedtoaddDoo::loadController(CoreController)inboostrapandthen itworks,sinceDooControllerispartofDooFwanditsaddedinDooclass.Youdontneedtoloaditone moretime.

Drewish 1Oct,2009

IreallyappreciatetheworkMil0s,ItrulydobutIcouldntgetthistoworkeitherandIhavebeenusingDoo Frameworkforawhile.Icantfigureitout. Ididexactlywhatyousaidsameerrorasguyabove ifiputtheloadcontrollerinthemaincontrollerigetapagefulloferrors.

Roman 1Oct,2009

Theredirect()methodhadbettercallexit()aftersendingtheheader,Ivefoundout.

Mil0s 1Oct,2009

Well,Iwillpostcodethatworkssothatwillsolveallproblems,staytunedIwillpackexampleofappand uploadithere.

Chuck 5Oct,2009

Yeah,Iattemptedthistoowiththesameresult.Also,isthistutorialforthelatestSVNtrunk?Idontbelieve Doo::sessionisfoundinthe1.2version.

Leng 5Oct,2009

DooSessionisnotin1.2.Itcanbefoundinthelatesttrunk

Roman 11Oct,2009

Milos,Ihaveacoupleofquestions: WhydoweneedtoDoo::loadController(CoreController)?WhycantthefilethatdefinesMainController includethefilethatdefinesCoreController? WhatbenefitdoyougetfromusingappendFile(),etc.toincludeJSfilesasopposedtospecifyingthemdirectly intemplatefiles?

MilosKovacki 11Oct,2009 +1

Youcandowithincludeaswell. ItsjustmyfunctionforaddingJStothesomepage,wellthereisnothatmuchbenefitbutIliketoheave alljsandandcssdoneinside tags. Itsjustsimplefunctiontoaddsomejs/cssinheaderofdocument.

stanley 20Oct,2009

Sameerror.Butitislooklikefixedbyadding require_once(Doo::conf()>BASE_PATH.controller/DooController.php) Doo::loadController(CoreController) inindex.phpscript.FrommypointofviewwhenwetrytoloadCoreControllerinindex.phpDooControlleris notloadedyet.

Leng 20Oct,2009

yesindeed,youhavetoloadDooControllerbeforeloadingthechildrenclassthatextenditifyoudoitmanually inindex.php

Roman 15Dec,2009

Manuallyasopposedtoturningonautoloadingtheframeworkclasses?

Deepak 16Dec,2009 +1 HiMilos, WhathappenifIaccesstheurllikehttp://www.mysitename.com/.InthatcaseMainController,indexwould neverwecalledandintializethosesessionsettings.Itthatright?? Wecanalsoinializeallthecoreobjectsinindex.php.

MilosKovacki 17Dec,2009 +1

Itwouldcallcontrolleryouasignedinroutes,andthatMainControllerjustextendsCoreControllersoall settingsandsessionwouldbeinitialized. Youjustextendyourcontrollerstosupercontroller.

taj 9Feb,2010 +1 TheonlywayIcanloadtheCoreControllerinthebootstrapwithoutgettingtheDooControllernotfounderror isbyloadingDooControllermanuallyfirst,afterIsetDoo::conf()>set($config). Doo::loadCore(controller/DooController) Doo::loadController(CoreController) ButIguessthisisthewrongwayofdoingit,right?

MilosKovacki 9Feb,2010 +1

Ijusthave: Doo::loadController(CoreController) inmyboostrap,andeverythingworksok,myCoreControllerislocatedinprotected/controllerdir,Iwill makeonedemotoshow.

Intellidance 12Mar,2010

Hi@all,

TheProblemorTrickistoaktivatethe__autoloadelementonindex.phptoautoloadallneedetclasses. IfyoucommentoutthisfunctionthemissedKlassisloadetandyouhaven`tanyerror

rd 28Jun,2010

HeyMilos, whatdoyoumeanbyinmybootstrapcanyoupleasespecifyaspecificfile/locationetc.?

MilosKovacki 7Jul,2010

Bootstrapismyindex.phpinpublicdirectory,heisinitializingDooFramewrok. Thatfileisjustincludingconfigsandthen: Doo::app()>route=$route Doo::app()>run()

KeithLoy 25Jul,2010

Warning:ThisisonlymyseconddayusingDooPHP.Willsomeonemoreadvancedpleasedoublecheckmy instructions. IfollowedMilosKovackisinstructionsexplicitlyandranintoseveralerrors.ThisishowIsuccessfullygotthis workingwiththeversionofDooPHPIcheckedoutfromsvnyesterday. Error1:ClassDooControllernotfound File:index.php Reason:autoloadingneedstobeturnedon. Fix:Uncommentfunction__autoload($classname) Error2:Undefinedproperty:DooConfig::ROOT_DIR File:controllers/CoreController.php Reason:ROOT_DIRappearstohavebeenrenamedtoBASE_PATH Fix:change$this>_basePath=Doo::conf()>ROOT_DIRto

$this>_basePath=Doo::conf()>BASE_PATH Error3:Undefinedproperty:DooConfig::$host File:controllers/CoreController.php Reason:hostappearstoberenamedAPP_URL Fix:change$this>host=Doo::conf()>hostto $this>host=Doo::conf()>APP_URL Error4:Undefinedproperty:DooConfig::$host(differentthenerror3) File:controllers/CoreController.php Reason:hostappearstoberenamedAPP_URL Fix:$this>_view>host=Doo::conf()>hostto $this>_view>host=Doo::conf()>APP_URL

guynamedkeith 25Jul,2010

IbelieveIhavesolvedalltheissues.Iamtryingtopostmyfixes,butamgettingservererrors.Mosterrorsare stemmingfromthechangesinDooPHPallowingtheapppathtobeinadifferentplace.

guynamedkeith 25Jul,2010

Warning:ThisisonlymyseconddayusingDooPHP. IfollowedMilosKovackisinstructionsexplicitlyandranintoseveralerrors.ThisishowIsuccessfullygotthis workingwiththeversionofDooPHPIcheckedoutfromsvnyesterday. Error1:ClassDooControllernotfound File:index.php Reason:autoloadingneedstobeturnedon. Fix:Uncommentfunction__autoload($classname) Error2:Undefinedproperty:DooConfig::ROOT_DIR File:controllers/CoreController.php Reason:ROOT_DIRappearstohavebeenrenamedtoBASE_PATH Fix:change$this>_basePath=Doo::conf()>ROOT_DIRto $this>_basePath=Doo::conf()>BASE_PATH Error3:Undefinedproperty:DooConfig::$host File:controllers/CoreController.php Reason:hostappearstoberenamedAPP_URL

Fix:change$this>host=Doo::conf()>hostto $this>host=Doo::conf()>APP_URL Error4:Undefinedproperty:DooConfig::$host(differentthenerror3) File:controllers/CoreController.php Reason:hostappearstoberenamedAPP_URL Fix:$this>_view>host=Doo::conf()>hostto $this>_view>host=Doo::conf()>APP_URL

Bint 25Aug,2010

guynamedkeith,thankyouforthetips! ThisishowIsuccessfullygotthisworkingandintegratedwithmycode

SonDoHa 28Aug,2010

Iused DooUriRouter::redirect($url) Butitdoesnotwork.Cananyonehelpme?Thankyou!

Bill 2Sep,2010

SinceareferencetothecurrentsessionisalwaysavailableviaDoo::session(),whydoyousaveittotheinstance member,_session?

MilosKovacki 2Sep,2010

@SonDoHa

Whatistheerroryouaregetting,andonemorethingseeifyouaresendinganythingtotheclientsinceyou mustusethatbeforeyousendanydata. @Bill BecauseinfirstversiontherewasnoDoo::session()Ithink,butofcourseyoucanalwaysuse Doo::session().

Ganesh 9Nov,2010

DooControllerisincludedonlyduringrun().butinyourexample,wereincludingtheCoreControllereven beforerun()so,itwontbeabletoextenddoocontroller(whichisneverloaded). so,idontseethisexampleworking.ifimwrong,@Milos,canyoupleasepostasimpledemo.

MilosKovacki 15Nov,2010

YoujustneedtoloadcontrollerinbootstrapwithDoo::loadController(CoreController) Sothiswouldbeexampleofyourbootstrap(index.php) [php] ini_set(display_errors,1) include./protected/config/common.conf.php include./protected/config/routes.conf.php include./protected/config/db.conf.php #Justincludethisforproductionmode //include$config['BASE_PATH'].deployment/deploy.php include$config['BASE_PATH'].Doo.php include$config['BASE_PATH'].app/DooConfig.php #Uncommentforautoloadingtheframeworkclasses. /*function__autoload($classname){ Doo::autoload($classname) }*/ Doo::conf()>set($config) #removethisifyouwishtoseethenormalPHPerrorview. include$config['BASE_PATH'].diagnostic/debug.php

#databaseusage //Doo::useDbReplicate()#fordbreplicationmasterslaveusage //Doo::db()>setMap($dbmap) //Doo::db()>setDb($dbconfig,$config['APP_MODE']) //Doo::db()>sql_tracking=true#fordebugging/profilingpurpose Doo::app()>route=$route Doo::loadController(CoreController) #UncommentforDBprofiling //Doo::logger()>beginDbProfile(doowebsite) Doo::app()>run() [/php]

Ganesh 24Nov,2010

butcorecontrollerextendsdoocontrollerwhichisnotloadedtillyourun().theexamplewontwork.
Search...

OurSponsors ChooseLanguage
English Russian Deutsch Espaol Arabic Portugese

PopularTags

FindTutorial
Easy Normal Difficult

Categories
Demos&Snippets(4) News&Updates(2) Screencast(1) Tutorials(17)

RecentComments
kukatonIntrotoDooPHPslides(PHPMalaysiameetup2011) MilosKovackionUsingDooTranslatorfortranslation WebdevGreeceonUsingDooTranslatorfortranslation MilosKovackionHandlingsessionswithoutApachesessions TonCreateasimpleToDoListinDooPHPPart2

Blogroll
DevelopmentBlog Documentation Forum GoogleCode Home About Contact Forum AllcontentscopyrightLearnDooPHP.Allrightsreserved.ThemedesignbyWebKreation.

Anda mungkin juga menyukai