Sie sind auf Seite 1von 11

LearnDooPHP

Login Home About Contact Forum SUBSCRIBETORSS LearnDooPHP:HighperformancePHPframework 21Sep2009

byRichard 16Comments Tutorials beginner,database,demo,easy,model,ORM,tutorial

CreateasimpleToDoListinDooPHPPart1
Introduction
ThistutorialwillguideyouthroughallthestepsrequiredtogetstartedusingDooPHPthroughthecreationofasimple ToDoListApplication.Thetutorialissplitintoanumberofsectionsandyouwillneedtoreadtheguidefromthe begininginordertounderstandwhatsgoingon. Part1GettingStarted Part2TheSignupForm Part3UserAuthenticationComingSoon Part4TheToDoListComingSoon Part5AddingAJAXFunctionalityComingSoon

OurObjectiveforthisTutorial
InthistutorialwewillbecreatingasimpleToDooListManagerWebApplicationandthroughthecourseofthe tutorialwewillbecoveringthefollowingtopics: ObtainingandDeployingtheBaseDooPHPApplication SettinguptheMySQLDatabase

CreatingtheModels CreatingtheToDoManagerApplication SignupForm UserAuthentication UserHomePage TaskActions EnhancingyouapplicationwithAJAX Youcanviewademoofthefinishedprojecthere.Somescreenshotsofthefinalapplicationarealsoincludedbelow: TODO:IMAGESTOGOINHEREWHENIFINISH! TODO:UPDATETHEDEMOLINKABOVE

ObtainingandDeployingtheBaseDooPHPApplication
BeforeyoucanbeginworkingontheToDooListManageryouwillneedaccesstoaWebServer.Thisguideassumes youalreadyhavewebhostingorbetteryetrunningadevelopmentserverlocallywhichsupportsPHP5andaMySQL. IfyoudonotyethaveyourownservertousethenIsuggesttakingalookatusingaWAMPserveronyourlocal system:http://www.wampserver.com/en/. YouwillalsobeneedingacopyofDooPHP.Youcandownloadthelatestreleasefromhere(forthisdemoIamusing Version1.2).IFyouwanttousetheverylatestsourceforDooPHPtakealookanddownloadthecodefromhere: DooPHPonGoogleCode Onceyouhavedownloadedoneofthecompressedpackagesopenitandcopythedooframeworkfolderintothe projectsrootfolderandthecontentsoftheappfolderintoyourpublic_htmlfolder.Thisshouldthengiveyouafolder structuresimilartothefollowing: X:\todomanager\ dooframework\ public_html\ global\ protected\ tools\ .htaccess.example index.php IntheaboveexampleapachehasbeenconfiguredtohavetheDOCUMENT_ROOTsetto x:\todomanager\public_html\withtheprojectrootthereforebeing:x:\todomanager\ AtthispointweneedtomakeafewchangestotheconfigurationofDooPHPinorderforittoworkunderthefile structurebeingusedforthisdemo.Thereforeyoufirstneedtoopenthefile public_html\protected\config\common.conf.phpandmakethefollowingchanges: 1. Changethedate_default_timezone_set()toyourownlocaltimezone 2. Changethe$config[SITE_PATH]and$config[BASE_PATH]to:
1 . $ c o n f i g [ ' S I T E _ P A T H ' ] = d i r n a m e ( d i r n a m e ( d i r n a m e ( _ _ F I L E _ _ ) ) ) . D I R E C T O R Y _ S E P A R A T O R 2 . $ c o n f i g [ ' B A S E _ P A T H ' ] = d i r n a m e ( $ c o n f i g [ ' S I T E _ P A T H ' ] ) . D I R E C T O R Y _ S E P A R A T O R . ' d o o f r a m e w o r k ' . D I R E C T O R Y _ S E P A R A T O R

3. Savethefile TheSITE_PATHisthefoldercontainingtheindex.phpfile(inourcasex:\todomanager\public_html).The BASE_PATHisthelocationofthedooframeworkwhichinthisexampleisx:\todomanager\dooframework\ Ifyouhavesetthisallupcorrectlythenyoushouldnowbeabletovisithttp://localhost/(ofcourseyoushouldchange thisifyoursiteishostedsomewhereotherthanlocalhost)andseeawelcomepageprovidedwithDooPHPtellingyou ItWorks!.Ifyoudonotseethischeckyouapache/phperrorlogsandcheckeverythingiscorrect.Ifyouarestill havingproblemsyoumayfindsomehelpintheforums. Nowyouarereadytocreatethedatabaseusedfortherestofthistutorial.

SettinguptheMySQLDatabase
OurToDooListManagerwillallowuserstosignupandregisterforanaccount.Thiswillrequirethemtopicka usernameandpasswordwhichwewilllaterusetoauthenticatetheuser.Inadditionwewillaskthemfortheiractual namesowecandisplaythistothemwhentheyhaveloggedin. EachregistereduserwillbeallowedtohaveasingleToDoListandeachusersToDoListwillbeprivatefromallthe otherusers.Wewillthereforerequire: AUsertabletohold: id username password name AndanothertableToDoItemtohold: id user_id task completed TherewillbeasimpleonetomanyrelationshipbetweenUsersandToDoItemswitheachuserabletohavemanyTo DoItems. Ihavealsodecidedtokeepthisdemoassimpleaspossiblesoalthoughwecouldcollectotherinformationlikethedue dateforataskandprovidingmultipleToDoListsperuserwewillfornowleavesuchextrafeaturesalonealthoughI encourageyoutotryaddingthesefeaturesyourselfasalearningexercise. InthisexampleIamusingadatabasecalledToDoManagerandtheSQLtocreatethetablesaboveisshowbelow:
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . C R E A T E T A B L E ` T o D o M a n a g e r ` . ` U s e r ` ( ` i d ` I N T N O T N U L L A U T O _ I N C R E M E N T P R I M A R Y K E Y , ` u s e r n a m e ` V A R C H A R ( 2 0 ) N O T N U L L , ` p a s s w o r d ` C H A R ( 3 2 ) N O T N U L L , ` n a m e ` V A R C H A R ( 4 0 ) N O T N U L L ) C R E A T E T A B L E ` T o D o M a n a g e r ` . ` T o D o I t e m ` ( ` i d ` I N T N O T N U L L A U T O _ I N C R E M E N T P R I M A R Y K E Y , ` u s e r _ i d ` I N T N O T N U L L ,

1 1 . ` t a s k ` V A R C H A R ( 2 5 5 ) N O T N U L L , 1 2 . ` c o m p l e t e d ` B I T ( 0 ) N O T N U L L 1 3 . )

YouwillneedtoruntheaboveSQLstatmentsonyourSQLserver(Iassumeherethatyouknowhowtodothis). AlsonotethatthesynxtaxaboveisintendedforMySQLandhasnotbeencheckedagainstotherdatabaseengines. Thereforeyoumayneedtochangeitslightlytouseitwithyourdatabase.

CreatingtheModels
NowthatwehaveourdatabasecreatedweneedtoletDooPHPknowaboutitsexistance.Todothiswemustfirst opentheDBconfigurationfilefoundinpublic_html\protected\config\db.conf.php. Youwillneedtouncommentthelasttwolinesstarting$dbconfig.Youmustalsospecifyyourdatabasescredentials.
1 . $ d b c o n f i g [ ' d e v ' ] = a r r a y ( ' l o c a l h o s t ' , ' T o D o M a n a g e r ' , ' u s e r n a m e ' , ' p a s s w o r d ' , ' m y s q l ' , t r u e ) 2 . $ d b c o n f i g [ ' p r o d ' ] = a r r a y ( ' l o c a l h o s t ' , ' T o D o M a n a g e r ' , ' u s e r n a m e ' , ' p a s s w o r d ' , ' m y s q l ' , t r u e )

Intheabovecodeyouwillneedtosetyourdatabasessettings.Theparametersare:HostName,DatabaseName, Username,Password,DatabaseEngineandlastelyifyouwanttouseapersistentconnection. DooPHPallowsustodefineboththedevelopmentandproductionsettingstobeusedforaccessingthedatabase.In Search... Random Share JoinChat thisexamplewewillassumethedetailsarethesamebutinrealworldapplicationsyouwilllikelyhavedifferent databasesettingsforthedevelopmentandproductionenvironments.DooPHPdeterminswhichsettingtousebasedon the$config["APP_MODE"]settingdefinedinthefilepublic_html\protected\config\common.conf.php.Thisis currentlysettodefaulttodev(developer)mode. WemustalsoinformDooPHPabouttherelationshipswhichexistinourdatabase.Weonlyhaveonerelationshipin thisdemowhichisamappingofoneUsertoManyToDoItem(ie.OneToMany).TotellDooPHPaboutthisyou needtoplacethefollowingcodejustabovethe$dbconfiglineswejustdefinedabove.
1 . $ d b m a p [ ' U s e r ' ] [ ' h a s _ m a n y ' ] [ ' T o D o I t e m ' ] = a r r a y ( ' f o r e i g n _ k e y ' = > ' u s e r _ i d ' ) 2 . $ d b m a p [ ' T o D o I t e m ' ] [ ' b e l o n g s _ t o ' ] [ ' U s e r ' ] = a r r a y ( ' f o r e i g n _ k e y ' = > ' i d ' )

ThisisessentiallytellingDooPHPthataUserhasmanyToDoItemswhereeachToDoItemhasauser_idwhich belongstoeachUserwhosidmatchesit.e.g.ifaUserhasanIDof7thenanyToDoItemwithauser_idequalto7 belongstothisuser.TolearnmoreaboutdefiningrelationshipsinDooPHPpleaserefertotheModelGuide. WemustalsoletDooPHPknowthatthisapplicationisutilisingadatabaseandtothereforeloadthesettingstoachieve this.Thereforeweneedtoopenthepublic_html\index.phpfileanduncommentthefollowinglinesofcode:


1 . 2 . 3 . 4 . 5 . i n c l u d e ' . / p r o t e c t e d / c o n f i g / d b . c o n f . p h p ' / * . . . * / D o o : : d b ( ) > s e t M a p ( $ d b m a p ) D o o : : d b ( ) > s e t D b ( $ d b c o n f i g , $ c o n f i g [ ' A P P _ M O D E ' ] ) D o o : : d b ( ) > s q l _ t r a c k i n g = t r u e

NowthatDooPHPknowsaboutourdatabaseswecanmakeuseoftheModelGeneratortoolsuppliedwiththeBase applicationintheDooPHPdownloadpackagetogeneratethemodelsautomaticallyforus.Simplynavigatebacktothe DooPHPwebsitehttp://localhost/andclickontheGenerateModelslink.Atthispointyoumaybeaskedfora usernameandpassword,itscurrentlysettoadminand1234. Ifthemodelgenerationworksasexpectedthenitshouldreportthecreationof2phpfiles: Todoitem.php

User.php Thesefilescanbefoundinpublic_html\protected\model\ifyouwishtotakealookatthem.Ifyouarenotinformedof themodelscreationpleasecheckoverthepreviousstepsandensureyouhavedoneeverythinglisted.Ifyouarestill havingproblemstryaskingforsomehelpintheforums.

CreatingtheToDoManagerApplication
NowthatwehavegottheBaseDooPHPapplicationsetupandwehavealsosetupandconfiguredourModelswecan startthinkingabouthowuserswillinteractwithourWebApplication. Wewillneedtoprovideuserswithanumberofpossibleactionstoperformandthesewillbesplitbetweenanumber ofControllerswhichwillgroupsimilaractionstogether.Userswillbeabletocarryoutthefollowingactionsonthe site: Signupforanaccount Login Logout ViewtheirownToDoList Addnewtaskstotheirlist Updatethestatusofaspecifiedtask(Incomple,Completed) Deleteatask Wewillthereforebreakthisdowninto2controllers.Onetohandeluserregistrationandauthenticationactivitiesand anothertomanagetheusersToDoList JustbeforewestartworkingontheseControllerswemustdeletesomefilesprovidedintheBaseApplicationwhich wenolongerneed.Thereforedeletethefollowingfiles public_html\toolsDeletethisfolder public_html\global\css\demo.cssDeletethisfile public_html\protected\config\formsDeletethisfolder public_html\protected\controller\*Deleteallfilesinthisfolder public_html\protected\viewc\*Deleteallfilesinthisfolder Youcanalsoopenupthepublic_html\protected\config\routes.conf.phpfileanddeleteallthelinesofcodebelowthe $admin=array(admin=>1234)(andthislineaswell)butkeeptheclosing?>phptag. Wearenowreadytostartcreatingoursignupform Part2CreatingtheSignupForm

16Comments
PreviousPost NextPost

mhdex 21Sep,2009 +1 whoaprettydetailed,nice!Lookingforwardfortheappdemoandsomescreenshots.Willthedemobeinthe demosectionofhttp://www.doophp.com?

zares 21Sep,2009 1 ThanksRichard. Thereisasmallremarkthecentralblockinthiswpthemeinitiallynotwide,andinadditionstealswidthofa content,thereforedoconclusions

Richard 21Sep,2009 +1

IllcontactlengandseeifhecanaltertheWPthemeorpossiblydoitmyselfalthoughImnotsureifI haveaccesstodothisatthemoment. Imhopinglengcanputthetutorialdemointothedemosectionofthesitelaterforpeopletotryoutand willalsoprovideazipfilecontainingthedemoforyoutodownload.

Leng 21Sep,2009 +3 Heyright,gottamodifythisWPthemeasap.

Richard

21Sep,2009

Ithinkitsgoingtobeawidthbeinga%and/oramin/maxwidthproperty.OnmylargermonitorsIhave noproblemswithtextgoingovertherighthandbarbutonthislaptopwithsmallerscreenresthecontent spansovertherightcolumn.

ai 22Sep,2009

Richard 22Sep,2009

http://translate.google.com/translate? prev=hp&hl=en&js=y&u=http%3A%2F%2Flearn.doophp.com%2F2009%2F09%2Fcreateasimpleto dolistindoophppart1%2F&sl=en&tl=zhCN&history_state0=

img 12Dec,2009

!Greatworks! thanksalot!

soso_86_2@yahoo.com 4Jan,2010

whenyouwillposttheothers?

Richard 7Jan,2010

ImafraidthatrightnowIcannotgiveananswerbutitwillhopefullyhappensoon.

Carlos 8Jan,2010 +1 Tagthisastutorial,itdoesntcomeupinthesearchforthatkeyword.Itdoesntcomeupingoogle(doophp tutorial)either.

aboshooq 16Feb,2010

DearRichard, Icouldntgetitwork,,relatedtoroutingissue!! itsaysomethinglikethis: ERROR404notfound ThisishandlerbyaninternalRouteasdefinedincommon.conf.php$config['ERROR_404_ROUTE'] Yourerrordocumentneedstobemorethan512bytesinlength.IfnotIEwilldisplayitsdefaulterrorpage. Givesomehelpfulcommentsotherthan404 AlsocheckoutthelinkspageforalistofURLsavailableinthis demo. hereistheurlaf_do/app/index.php/gen_model

aboshooq 16Feb,2010

Dear*, myissueisbeenfixed, itwasrelatedto$config['SUBFOLDER']and$config['APP_URL']. Ifixitbutnotusingtheproperway.. >$config['SUBFOLDER']=/fahad/af_do/app/ Thanku*

FarhadSakhaei 18Apr,2010

ThanksForTheGuide Justtherewasabitmistake: $dbconfig['prod']=array(localhost,ToDoManager,password,password,mysql,true) Shouldbe: $dbconfig['prod']=array(localhost,ToDoManager,username,password,mysql,true)

Richard 21Apr,2010 +1

Thanks,Justcorrectedthem

wrlee 16Jul,2010

IamlookingforwardtotipsonAjaxusage.

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.

Das könnte Ihnen auch gefallen