亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

理解PHP中的MVC編程之控制器

瀏覽:3日期:2024-02-05 17:53:41

簡單來講,控制器的作用就是接受請求。它使用獲取的方法,在這里是通過URI,載入一個功能模塊來刷新或者提交一個表述層。控制器將使用$_GET自動全局變量來判斷載入哪一個模塊。

一個請求的例子,看起來像這樣:

http://example.com/index.php?module=login

這看起來很簡單,但是在實現(xiàn)的過程中卻不是。這里是幾個控制器能識別的argument部分:

module定義了使用哪一個模塊,如users模塊 class定義了使用哪一個功能類,如你想讓用戶login還是logout event定義了使用哪一個具體事件

這樣一個更復(fù)雜的例子可以解釋上面的各個argument最終組成的請求URL:

http://example.com/index.php?module=users&class=login

這段請求告訴控制器應(yīng)該載入users模塊,然后是login類,最后因為沒有定義具體事件,所以運(yùn)行l(wèi)ogin::__default()默認(rèn)事件。

以下是具體代碼部分:

<?php  /*** index.php** @author Joe Stump <[email protected]>* @copyright Joe Stump <[email protected]>* @license http://www.opensource.org/licenses/gpl-license.php* @package Framework */

 require_once('config.php');

 // {{{ __autoload($class) /*** __autoload** Autoload is ran by PHP when it can't find a class it is trying to load.* By naming our classes intelligently we should be able to load most classes* dynamically.** @author Joe Stump <[email protected]>* @param string $class Class name we're trying to load* @return void* @package Framework */

 function __autoload($class) {$file = str_replace('_','/',substr($class,2)).'.php'; require_once(FR_BASE_PATH.'/includes/'.$file); } // }}}

 if (isset($_GET['module'])) {$module = $_GET['module'];if (isset($_GET['event'])) { $event = $_GET['event'];} else { $event = '__default';}

 if (isset($_GET['class'])) {$class = $_GET['class']; } else {$class = $module; }

 $classFile = FR_BASE_PATH.'/modules/'.$module.'/'.$class.'.php'; if (file_exists($classFile)) {require_once($classFile);if (class_exists($class)) { try {$instance = new $class();if (!FR_Module::isValid($instance)) { die('Requested module is not a valid framework module!');}

$instance->moduleName = $module;if ($instance->authenticate()) { try {$result = $instance->$event();if (!PEAR::isError($result)) { $presenter = FR_Presenter::factory($instance->presenter,$instance);

if (!PEAR::isError($presenter)) { $presenter->display();} else { die($presenter->getMessage());} }} catch (Exception $error) { die($error->getMessage());} } else {die('You do not have access to the requested page!'); }} catch (Exception $error) {  die($error->getMessage()); } } else {die('An valid module for your request was not found');  } } else {die('Could not find: $classFile');  } } else {die('A valid module was not specified');}

?>;

接下來是以上代碼具體的注釋:

載入“config.php”

定義__autoload()函數(shù)。這是PHP5里面的一個新函數(shù),方便動態(tài)地載入各個類。

如果一個argument被定義,那么載入相關(guān)的模塊、類和具體事件

接下來就是一些判斷以及錯誤的具體操作

最后一切無誤后就載入表述層

【友好URL】

如果你覺得上面例子講到的請求URL讓你覺得不舒服的話,那么就用mod_rewrite來實現(xiàn)友好URL吧。接下來是作者給這個框架寫的實際重寫標(biāo)準(zhǔn)代碼:

RewriteEngine On

# Change the URI here to whatever you want your homepage to be

RewriteRule ^/$ /index.php?module=welcome [L,QSA]

# Changes /index.php?module=welcome to /welcome

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f

RewriteRule ^/([^/]*)$ /index.php?module=$1 [L,QSA]

# Changes /index.php?module=users&class=login to /users/login

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f

RewriteRule ^/([^/]*)/([^/]*)$ /index.php?module=$1&class=$2 [L,QSA]

# Changes /index.php?module=users&class=login&event=foo

# to /users/login/foo.html

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f

RewriteRule ^/([^/]*)/([^/]*)/([^/]*).html$

/index.php?module=$1&class=$2&event=$3 [L,QSA]

Extending the Controller

【擴(kuò)展控制器】

擁有一個集中控制器的一點好處就是你加入一些功能后,馬上就能通過控制器體現(xiàn)出來。以下是幾個可以擴(kuò)展一下這個控制器的點子,使這個框架的整體能力更加強(qiáng)大: 你可以使用PHP5里一個新東西SoapServer來自動檢測一個請求是否為SOAP

你可以使用控制器來過濾所有的自動全局變量如$_GET和$_POST以防止惡意HTML代碼等

你可以使用控制器即時地轉(zhuǎn)換表述層,比如從默認(rèn)的方式轉(zhuǎn)到PDF方式

你可以直接在控制器中加入緩存機(jī)制,這樣的好處是應(yīng)用程序整體都能使用到緩存以提高效率

當(dāng)然,需要注意一點的是,你在控制器中所增加的功能將體現(xiàn)在程序全局。如你想過濾所有的自動全局變量,但是很多應(yīng)用程序的管理員需要使用到一些HTML代碼,反而成為一件棘手的事情(譯者注:本人的想法是可以加一個if條件語句,在加載特定模塊時不應(yīng)用過濾功能即可)。

標(biāo)簽: PHP
主站蜘蛛池模板: 麻豆传媒视频入口 | 女人被免费网站视频在线 | 一级黄色欧美 | 伊人影院中文字幕 | 自拍视频第一页 | 亚洲欧美精品综合中文字幕 | 亚洲天天综合色制服丝袜在线 | 高清中国一级毛片免费 | 亚洲精品一区专区 | 欧美经典成人在观看线视频 | 玖玖爱zh综合伊人久久 | 天堂亚洲欧美日韩一区二区 | 亚洲高清视频在线播放 | 日本sese| 日本免费新一区二区三区 | a三级毛片| 免费毛片在线视频 | 级毛片| 最新国产在线观看 | 亚洲欧美一区二区三区在线播放 | 午夜网站在线观看 | 人妖与黑人做爰 | 黄色大片网站 | 国产一区国产二区国产三区 | 欧美日韩一卡二卡 | 精品高清写真视频在线 | 国产在线日韩在线 | 国产女人性做爰视频 | 色婷婷激婷婷深爱五月老司机 | 国产成人福利美女观看视频 | 韩日免费视频 | 亚洲精品ai换脸一区二区三区 | 91porn丫九色 | 精品一区二区三区免费站 | 日韩精品一区二区三区中文3d | 三级网站日本 | 日本免费久久久久久久网站 | 大乳一级一区二区三区 | 欧美黄色片 一级片 | 国产精品大全国产精品 | 欧美成人性色xxxx视频 |