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