ATypicalCake
PHPRequest
We'vecoveredthebasicingredientsinCakePHP,solet'slookathoweachobjectworkstogethertocompleteabasicrequest.Continuingwithouroriginalrequestexample,let'simaginethatourfriendRicardojustclickedonthe"BuyACustomCakeNow!"linkonaCakePHPapplication'slandingpage.
Helpers
Layout
ViewViewElementsElementsElements
8
7
Models
DataSourcesBehaviors
4
Client
5
1
Dispatcher
2
Routes
3
Controller
6
Components
Figure2.TypicalCakeRequest.Black=requiredelement,Gray=optionalelement,Blue=ca!back1.Ricar
doclicksthelinkpointingtohttp://www.example.com/cakes/buy,andhisbrowsermakesarequesttoyour
webserver.2.TheDispatcherisaCakePHPcoreclassthatinterpretstheURL,usesRoutestocreatethecorrectcontrollerinstance,callstheappropriatecallbacks,andcallsthecorrectcontrollermethodstorende
rarequestedaction.
CakePHPv1.2
17
3.Usingroutes,arequestURLismappedt
oacontrolleraction(amethodinaspeciccontrollerclass).Inthiscase,it'sthebuy()methodoftheCakesController.Thecontroller'sbeforeFilter()callbackiscalledbeforeanycontrolleractionlogicisexecuted.4.Thecontrollermayusemodelstogain
accesstotheapplication'sdata.Inthisexample,thecontrollerusesamodeltofetchRicardo'slastpurchasesfromthedatabase.Anyapplicablemodelcallbacks,behaviors,andDataSourcesmayapplyduringthisoperation.Whilemodelusageisnotrequired,allCakePHPcontrollersinitiallyrequireatleastonemodel.5.Afterthemodelhasretrievedthedata,itisreturnedtothecontroller.Modelcallbacksmayapply.6.Thecontrollermayusecomponentstofurtherrenethedataorperformotheroperations(sessionmanipulationorsendingemails,forexample).7.Oncethecontrollerhasusedmodelsandcomponentstopreparethedatasuciently,thatdataishandedtotheviewusingthecontroller'sset()method.Controllercallbacksmaybeappliedbeforethedataissent.Theviewlogicisperformed,whichmayincludetheuseofelementsand/orhelpers.Bydefault,theviewisrenderedinsideofalayout.8.Additionalcontrollercallbacks(afterRenderandafterFilter)maybeapplied.Thecomplete,renderedviewcodeissenttoRicardo'sbrowser.