理解PHP中的MVC編程之MVC框架簡介_第1頁
理解PHP中的MVC編程之MVC框架簡介_第2頁
理解PHP中的MVC編程之MVC框架簡介_第3頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、MVC?】、Controller”諧調(diào)地組成一個簡單應(yīng)用程序的概念。一輛汽車就是一個在現(xiàn)實生活中格外好的 MVC 例子。我們看車都看兩個 View顯示局部:內(nèi)部和外部。而這Controller把握者:司機。剎車系統(tǒng)、方向盤和其他操那里取得把握方法然后應(yīng)用到內(nèi)部和外觀View。MVC】單獨的把握器如index.php用來把握全部建立在參數(shù)懇求根底上的框架內(nèi)應(yīng)最小程度上GETfoofoo:barbar做的好處有:一個對應(yīng)全部應(yīng)用程序的接口合并并重復(fù)使用代碼【為什么要創(chuàng)立作者自己的 MVC 框架?】PHPMVCSolarPHP5Cake,PHPRoRRuby on RailsRuby。我自己對這兩個

2、框架都有一些PEAR,SmartyCakePaul。這些問題可能并不會讓你否認(rèn)它們,它們?!纠戏绞健縯emplate.txt件,它看起來像這樣: HYPERLINK “ :/ 轉(zhuǎn)載請注明出處?phprequire_once(”config.php”); / Other requires, DB info, etc.$APP_DB = ”mydb”;$APP_REQUIRE_LOGIN = false; / Set to true if script requires login$APP_TEMPLATE_FILE = ”foo.php”; / Smarty template$APP_TITLE

3、 = ”My Application”;if ($APP_REQUIRE_LOGIN = true) if (!isset($_SESSION”userID”) header(“Location: /path/to/login.php“);exit;$db = DB:connect(”mysql:/”.$DB_USER.”:”.$DB_PASS.”localhost/”.$APP_DB);if (!PEAR:isError($db) $db-setFetchMode(DB_FETCHMODE_ASSOC); else die($db-getMessage);/ Put your logic h

4、ere/ Output the templateinclude_once(APP_TEMPLATE_PATH.”/header.php”); include_once(APP_TEMPLATE_PATH.”/”.$APP_TEMPLATE_FILE);include_once(APP_TEMPLATE_PATH.”/footer.php”);?一個應(yīng)用程序都能適用于這個處理方法,比方我可以簡潔地將 template.txt 拷密的處理方法存在一些嚴(yán)峻的缺點:HTML、一些狀況下XMLSOAP,我該怎么辦?IMAPLDAP 包括編輯、升級和刪除?我該如何處理多級驗證vs.?我該如何啟用輸出緩存?

5、 HYPERLINK “ :/ 轉(zhuǎn)載請注明出處【方式】MVC碼:?phpclass myapp extends FR_Auth_Userpublic functionconstructparent:construct;public functiondefault/ Do something herepublic function delete public functiondestructparent:destruct;?或者輸出任何其他信息。把握器把握了全部的一切。LDAP,F(xiàn)R_Auth_LDAP。把握器可以識別某些輸出方法比方$_GET”output”PDFSOAPdel是理所固然的,但

6、是把握器同樣可以把握一局部緩存。但是一旦你轉(zhuǎn)換到了這樣一個概念,那么要轉(zhuǎn)回去將是件相當(dāng)困難的事情?!窘⒌讓印縋EARPEAR_ErrorPHP5類“Exception” 取代了 PEAR_ErrorPEAR_ErrorExceptionMVCException不能傳回錯誤。PEAR序類的層次看起來會像這樣:setFrom,toArray-FR_Object_DB 是一個小層面,給子類供給數(shù)據(jù)庫鏈接等功能-FR_Module又稱模塊、模型等等的底層類-FR_AuthFR_Auth_User 是一個驗證類,用來驗證全部需要驗證用戶是否登陸的模塊FR_Auth_No 是全部不需要驗證的模塊的“假驗

7、證類”-FR_Presenter 是全部用來處理載入和顯示應(yīng)用的底層類-FR_Presenter_SmartySmarty譯者注:這清楚就是打廣告嘛應(yīng)用程序并給他們除錯RESTMVCFR_ModuleFR_Presenter【代碼標(biāo)準(zhǔn)】或者你自己好好爭辯或思考一下代碼標(biāo)準(zhǔn)。MVC 編程的整體思想圍圍著兩點:代碼的可再利用性削減偶合和代碼的標(biāo)準(zhǔn)化。我推舉至少應(yīng)當(dāng)考慮到如下幾點:根底類的時候。定制一個標(biāo)準(zhǔn)前綴,用在全部的函數(shù)、類和全局變量上。不走運的是,PHP不支持“namespace命名空間”。所以要想避開混淆變量名和發(fā)生的沖突,“FR_”作為這樣的前綴?!揪帉懙讓印? config.phpin

8、dex.php includes/ Auth.php Auth/ No.php User.php Module.phpObject.phpObject/ DB.phpPresenter.phpPresenter/ common.php debug.php smarty.php Smarty/ modules/ example/ config.php example.phptpl/ example.tpltpl/ default/ cache/ config/templates/ templates_c/到很大的提升。includes都用一個配置文件,至少一個模塊文件和一個模板文件。全部的模塊包

9、含在modulestplconfig.php中樞配置文件,包含全部的全局配置變量。index.php把握器,在接下來的一篇文章中會具體表達(dá)。 object.php全部根底類的底層類,供給絕大局部類需要的功能。FR_Object_DB共同的特性。你完全可以把鏈接數(shù)據(jù)庫的功能放進(jìn)FR_Object,但是并不是全部FR_Object_DB爭辯它。?phprequire_once(”Log.php”);/*FR_Object*The base object class for most of the classes that we use in ourframework.Provides basic

10、logging and set/get functionality.*author Joe Stump HYPERLINK “mailto:“ package Framework*/abstract class FR_Object/*$log*var mixed $log Instance of PEAR Log*/protected $log;/*$me*var mixed $me Instance of ReflectionClass*/protected $me;/* construct*author Joe Stump HYPERLINK “mailto:“ access public

11、*/public functionconstruct$this-log = Log:factory(”file”,FR_LOG_FILE);$this-me = new ReflectionClass($this);/*setFrom*author Joe Stump HYPERLINK “mailto:“ access publicparam mixed $data Array of variables to assign to instancereturn void*/public function setFrom($data)if (is_array($data) & count($da

12、ta) $valid = get_class_vars(get_class($this);foreach ($valid as $var = $val) if (isset($data$var) $this-$var = $data$var;/*toArray*author Joe Stump HYPERLINK “mailto:“ access publicreturn mixed Array of member variables keyed by variable name*/public function toArray$defaults = $this-me-getDefaultPr

13、operties;$return = array;foreach ($defaults as $var = $val) if ($this-$var instanceof FR_Object) $return$var = $this-$var-toArray; else $return$var = $this-$var;return $return;/* destruct*authorJoe Stump HYPERLINK “mailto:“ *accesspublic*/returnvoidpublic functiondestructif ($this-log instanceof Log

14、) $this-log-close;?FR_Module的,主要功能是定義一個根本的驗證類如何工作。FR_Auth_No?phpabstract class FR_Auth extends FR_Module/ construct functionparent:construct;/ / authenticateabstract function authenticate;/ / destruct functionparent:destruct;/ ?module.php全部模塊的心臟?phpabstract class FR_Module extends FR_Object_Web/ pro

15、perties/*$presenter*Used in FR_Presenter:factory to determine which presentation(view)class should be used for the module.*author Joe Stump HYPERLINK “mailto:“ var string $presentersee FR_Presenter, FR_Presenter_common, FR_Presenter_smarty*/public $presenter = ”smarty”;/*$data*ew.Data set by the mod

16、ule that will eventually be passed to the vi*author Joe Stump HYPERLINK “mailto:“ var mixed $data Module datasee FR_Module:set, FR_Module:getData*/protected $data = array;/*$name*author Joe Stump HYPERLINK “mailto:“ var string $name Name of module class*/public $name;/*$tplFile*author Joe Stump HYPE

17、RLINK “mailto:“ var string $tplFile Name of template filesee FR_Presenter_smarty*/public $tplFile;/*$moduleName*author Joe Stump HYPERLINK “mailto:“ var string $moduleName Name of requested modulesee FR_Presenter_smarty*/public $moduleName = null;/*$TemplateFile*author Joe Stump HYPERLINK “mailto:“

18、var string $TemplateFile Name of outer template*/public $TemplateFile = null;/ / construct/* construct*author Joe Stump HYPERLINK “mailto:“ */public functionconstructparent:construct;$this-name = $this-me-getName;$this-tplFile = $this-name.”.tpl”;/ / defaultfied/* default*This function is ran by the

19、 controller if an event is not speciin the user”s request.*author Joe Stump HYPERLINK “mailto:“ */abstract public functiondefault;/ / set($var,$val)/*set*Set data for your module. This will eventually be passed toe thepresenter class via FR_Module:getData.*author Joe Stump HYPERLINK “mailto:“ param

20、string $var Name of variableparam mixed $val Value of variablereturn voidsee FR_Module:getData*/protected function set($var,$val) $this-data$var = $val;/ / getData/*getData*Returns module”s data.*author Joe Stump HYPERLINK “mailto:“ return mixedsee FR_Presenter_common*/public function getDatareturn

21、$this-data;/ / isValid($module)by rk”s/*isValid*Determines if $module is a valid framework module. This is usedthe controller to determine if the module fits into our framewomold. If it extends from both FR_Module and FR_Auth then it should begood to run.*author Joe Stump HYPERLINK “mailto:“ staticparam mixed $modulereturn bool*/public static function isValid($module)return (is_object($module) &$module instanceof FR_Module &$module instanceof FR_Auth);/ / destructpublic functiondestructparent:destruct;/ ?presenter.php表述層的核心。?phpclass FR_Presenter/ factory($type,FR_Module $module)/*

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論