<?php
/* 檢查是否爲開發環境並設置是否記錄錯誤日誌 */
function setReporting(){
if (DEVELOPMENT_ENVIRONMENT == true) {
error_reporting(E_ALL);
ini_set('display_errors','On');
} else {
error_reporting(E_ALL);
ini_set('display_errors','Off');
ini_set('log_errors','On');
ini_set('error_log',ROOT.DS. 'tmp' .DS. 'logs' .DS. 'error.log');
}
}
如圖所示:
/* 檢測敏感字符轉義(Magic Quotes)並移除他們 */
function stripSlashDeep($value){
$value = is_array($value) ? array_map('stripSlashDeep',$value) : stripslashes($value);
return $value;
}
function removeMagicQuotes(){
if (get_magic_quotes_gpc()) {
$_GET = stripSlashDeep($_GET);
$_POST = stripSlashDeep($_POST);
$_COOKIE = stripSlashDeep($_COOKIE);
}
}
如圖所示:
/* 檢測全局變量設置(register globals)並移除他們 */
function unregisterGlobals(){
if (ini_get('register_globals')) {
$array = array('_SESSION','_POST','_GET','_COOKIE','_REQUEST','_SERVER','_ENV','_FILES');
foreach ($array as $value) {
foreach ($GLOBALS[$value] as $key => $var) {
if ($var === $GLOBALS[$key]) {
unset($GLOBALS[$key]);
}
}
}
}
}
如圖所示:
/* 主請求方法,主要目的拆分URL請求 */
function callHook() {
global $url;
$urlArray = array();
$urlArray = explode("/",$url);
$controller = $urlArray[0];
array_shift($urlArray);
$action = $urlArray[0];
array_shift($urlArray);
$queryString = $urlArray;
$controllerName = $controller;
$controller = ucwords($controller);
$model = rtrim($controller, 's');
$controller .= 'Controller';
$dispatch = new $controller($model,$controllerName,$action);
if ((int)method_exists($controller, $action)) {
call_user_func_array(array($dispatch,$action),$queryString);
} else {
/* 生成錯誤代碼 */
}
}
如圖所示:
/* 自動加載控制器和模型 */
function __autoload($className) {
if (file_exists(ROOT . DS . 'library' . DS . strtolower($className) . '.class.php')) {
require_once(ROOT . DS . 'library' . DS . strtolower($className) . '.class.php');
} else if (file_exists(ROOT . DS . 'application' . DS . 'controllers' . DS . strtolower($className) . '.php')) {
require_once(ROOT . DS . 'application' . DS . 'controllers' . DS . strtolower($className) . '.php');
} else if (file_exists(ROOT . DS . 'application' . DS . 'models' . DS . strtolower($className) . '.php')) {
require_once(ROOT . DS . 'application' . DS . 'models' . DS . strtolower($className) . '.php');
} else {
/* 生成錯誤代碼 */
}
}
setReporting();
removeMagicQuotes();
unregisterGlobals();
callHook();
如圖所示:
接下來的操作就是在library中建立程序所需要的基類,包括控制器、模型和視圖的基類。
下面操作請點擊:封裝MVC(四)