理解PHP中的MVC編程之控制器
簡(jiǎn)單來講,控制器的作用就是接受請(qǐng)求。它使用獲取的方法,在這里是通過URI,載入一個(gè)功能模塊來刷新或者提交一個(gè)表述層。控制器將使用$_GET自動(dòng)全局變量來判斷載入哪一個(gè)模塊。
一個(gè)請(qǐng)求的例子,看起來像這樣:
http://example.com/index.php?module=login
這看起來很簡(jiǎn)單,但是在實(shí)現(xiàn)的過程中卻不是。這里是幾個(gè)控制器能識(shí)別的argument部分:
module定義了使用哪一個(gè)模塊,如users模塊 class定義了使用哪一個(gè)功能類,如你想讓用戶login還是logout event定義了使用哪一個(gè)具體事件
這樣一個(gè)更復(fù)雜的例子可以解釋上面的各個(gè)argument最終組成的請(qǐng)求URL:
http://example.com/index.php?module=users&class=login
這段請(qǐng)求告訴控制器應(yīng)該載入users模塊,然后是login類,最后因?yàn)闆]有定義具體事件,所以運(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里面的一個(gè)新函數(shù),方便動(dòng)態(tài)地載入各個(gè)類。
如果一個(gè)argument被定義,那么載入相關(guān)的模塊、類和具體事件
接下來就是一些判斷以及錯(cuò)誤的具體操作
最后一切無誤后就載入表述層
【友好URL】
如果你覺得上面例子講到的請(qǐng)求URL讓你覺得不舒服的話,那么就用mod_rewrite來實(shí)現(xiàn)友好URL吧。接下來是作者給這個(gè)框架寫的實(shí)際重寫標(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ò)展控制器】
擁有一個(gè)集中控制器的一點(diǎn)好處就是你加入一些功能后,馬上就能通過控制器體現(xiàn)出來。以下是幾個(gè)可以擴(kuò)展一下這個(gè)控制器的點(diǎn)子,使這個(gè)框架的整體能力更加強(qiáng)大: 你可以使用PHP5里一個(gè)新東西SoapServer來自動(dòng)檢測(cè)一個(gè)請(qǐng)求是否為SOAP
你可以使用控制器來過濾所有的自動(dòng)全局變量如$_GET和$_POST以防止惡意HTML代碼等
你可以使用控制器即時(shí)地轉(zhuǎn)換表述層,比如從默認(rèn)的方式轉(zhuǎn)到PDF方式
你可以直接在控制器中加入緩存機(jī)制,這樣的好處是應(yīng)用程序整體都能使用到緩存以提高效率
當(dāng)然,需要注意一點(diǎn)的是,你在控制器中所增加的功能將體現(xiàn)在程序全局。如你想過濾所有的自動(dòng)全局變量,但是很多應(yīng)用程序的管理員需要使用到一些HTML代碼,反而成為一件棘手的事情(譯者注:本人的想法是可以加一個(gè)if條件語(yǔ)句,在加載特定模塊時(shí)不應(yīng)用過濾功能即可)。
