初始化主要是使用了`init.php`類
代碼如下:
~~~
<?php
//初始化類
//判斷當前請求是否正常,如果不是通過index.php進行訪問的,就退出系統
if(!defined('ACCESS')) die('Hacking');
//實現初始化類
class Init{
//瀏覽器顯示的字符集
private static function initHeader(){
//uft-8字符集
header("Content-type:text/hmtl;charsetset=uft-8");
}
//目錄常量
private static function initConst(){
//先找到項目根目錄(應用根目錄),__DIR__可以找到本文件所在的目錄,再通過更目錄去定位其他的目錄
//根目錄下
defined('APP') or define('APP', str_replace('\\', '/', __DIR__));
defined('APP_ADMIN') or define('APP_ADMIN', APP.'/Admin/');
defined('APP_API') or define('APP_API', APP.'/Api/');
defined('APP_CORE') or define('APP_CORE', APP.'/Core/');
defined('APP_CONFIG') or define('APP_CONFIG', APP.'/Config/');
defined('APP_PUBLIC') or define('APP_PUBLIC', APP.'/Public/');
defined('APP_COMMON') or define('APP_COMMON', APP.'/Common/');
defined('APP_LOGS') or define('APP_LOGS', APP.'/Logs/');
//Admin目錄下
defined('ADMIN_MODEL') or define('ADMIN_MODEL', APP_ADMIN.'Model/');
defined('ADMIN_VIEW') or define('ADMIN_VIEW', APP_ADMIN.'View/');
defined('ADMIN_CONTROLLER') or define('ADMIN_CONTROLLER', APP_ADMIN.'Controller/');
//Api目錄下
defined('API_MODEL') or define('API_MODEL', APP_API.'Model/');
defined('API_VIEW') or define('API_VIEW', APP_API.'View/');
defined('API_CONTROLLER') or define('API_CONTROLLER', APP_API.'Controller/');
defined('EXT') or define('EXT', '.class.php');
defined('MODULE') or define('MODULE', 'Api');
}
//加載公共函數
private static function initFunction(){
require_once APP_COMMON.'function.php';
}
//自定義處理錯誤
private static function initError(){
//set_error_handler("error_handler");
}
//配置文件加載
private static function initConfig(){
//加載
$config=require_once APP_CONFIG.'config.php';
//全局化配置文件
$GLOBALS['config']=$config;
}
//使用自定義自動加載
private static function initAutoload(){
if (function_exists('spl_autoload_register')) {
require_once APP_CORE.'Autoload'.EXT;
spl_autoload_register(array('Autoload', 'AppCore'));
spl_autoload_register(array('Autoload', 'ApiController'));
spl_autoload_register(array('Autoload', 'ApiModel'));
spl_autoload_extensions('.php');
}else{
function __autoload($className){
//return Core::autoload($className);
}
}
}
//開啟session
private static function initSession(){
//修改session機制
}
//初始化url
/**
*
*/
private static function initUrl(){
//pathinfo模式
//獲取網址,取出pathinfo模式下的模塊,控制器和方法
$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
$path=explode("index.php", $url);
$_SESSION['argus']=array();
if((strpos($path[0], "http://")!==false) && $path[1]==""){ //控制器和方法缺省
$controller="Index";
$action="index";
}else if((strpos($path[0], "http://")!==false) && strpos($path[1], "/")==0 && strrpos($path[1], "/")==0 && strlen($path[1])>1){//控制器存在,方法缺省
//獲取請求的控制器和方法
$request=explode("/", $path[1]);
//一般將類名的首字母大寫,方法名小寫
$controller=ucfirst(strtolower($request[1]));
$action="index";
}else{
//獲取請求的控制器和方法
$request=explode("/", $path[1]);
//一般將類名的首字母大寫,方法名小寫
$controller=ucfirst(strtolower($request[1]));
$action=strtolower($request[2]);
//將傳遞過來的參數封裝到數組中
for($i=0;$i<count($request);$i++){
if($i>2 && $i%2 ==1 ){
$argus[$request[$i]]=$request[$i+1];
}
}
$_SESSION['argus']=$argus;
}
//為了局部變量在其他地方可以使用,將之定義為常量
define('CONTROLLER', $controller);
define('ACTION', $action);
}
//將用戶請求的控制器和方法分發
private static function initDispatch(){
//創建控制類的對象和調用其相關的方法
$controller=CONTROLLER; //類名
$action=ACTION; //方法名
//構造全類名
$controller .= "Controller"; //這里類文件和類名是一樣的
//實例化
$object=new $controller($_SESSION['argus']);
//調用方法
$object->$action();
}
//初始化方法
public static function run(){
//瀏覽器顯示的字符集
self::initHeader();
//目錄常量
self::initConst();
//加載公共函數
self::initFunction();
//系統錯誤
self::initError();
//配置文件加載
self::initConfig();
//類的自動加載,調用函數自定義spl_autoload_register()文件加載
self::initAutoload();
//開啟session
self::initSession();
//初始化url,獲取當前用戶的請求,其實就是執行確定的控制器的確定方法
self::initUrl();
//將用戶請的控制器和方法分發
self::initDispatch();
}
}
?>
~~~
>[warning]這里的**pathinfo模式暫時不支持傳參**,想要可以傳參,自己再補充一下,或者將跳轉的方式更改為傳統模式