index.php 主入口文件php
<?php
define('ISEXIST',true);
require "init.php";
$control = new Controller();
$control -> Run();
?>html
---------------------------------------------------------------------------------------------數組
init.php 文件php框架
<?php
if(!defined('ISEXIST'))
exit("請從入口文件運行程序");
header("Content-Type:text/html;charset=utf-8");
if(!defined('ROOT_PATH'))
//這裏動態的聲明,'\\'是轉義反斜線,默認'\'爲轉義字符
define('ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
require ROOT_PATH.'/a/config.php';
require ROOT_PATH.'/a/controller.class.php';
require ROOT_PATH.'/a/view.class.php';
require ROOT_PATH.'/a/model.class.php';
?>框架
----------------------------------------------------------------------------------------------ui
config.php 文件this
<?php
if(!defined('ISEXIST'))
exit("請從入口文件運行程序");
$C = array(
'URL_MODE'=>1,//url模式,1爲普通模式,2爲path_info模式
'DEFAULT'=>'welcome',//默認的控制器
'DEFAULT_ACTION'=>'index'//默認的方法
);
?>url
-----------------------------------------------------------------------------------------------spa
controller.class.php 文件htm
<?php
class Controller
{
public function Run()
{
$this->Analysis();
//開始解析URL得到請求的控制器和方法
$control = $_GET['con'];
$action = $_GET['act'];
$action = ucfirst($action);
//這裏構造出控制器文件的路徑
$controlFile = ROOT_PATH . '/Controllers/' . $control . '.class.php';
if(!file_exists($controlFile)) //若是文件不存在提示錯誤, 不然引入
{
exit("{$control}.class.php控制器不存在<br>". "請檢查: ".$controlFile."是否存在<br>");
}
include($controlFile);
$class = ucfirst($control); //將控制器名稱中的每一個單詞首字母大寫,來看成控制器的類名
if(!class_exists($class)) //判斷類是否存在, 若是不存在提示錯誤
{
exit("{$control}.class.php中未定義的控制器類" . $class);
}
$instance = new $class(); //不然建立實例
if(!method_exists($instance, $action)) //判斷實例$instance中是否存在$action方法, 不存在則提示錯誤
{
exit("$class類中不存在方法:". $action);
}
$instance->$action();
}
protected function Analysis()
{
//$GLOBALS['C']['URL_MODE'];
global $C; //包含全局配置數組, 這個數組是在Config.ph文件中定義的,global聲明$C是調用外部的
if($C['URL_MODE'] == 1)
//若是URL模式爲1 那麼就在GET中獲取控制器, 也就是說url地址是這種的 [url=http://localhost/index.php?c]http://localhost /index.php?c[/url]=控制器&a=方法
{
$control = !empty($_GET['con']) ? trim($_GET['con']) : '';
$action = !empty($_GET['act']) ? trim($_GET['act']) : '';
}
else if($C['URL_MODE'] == 2) //若是爲2 那麼就是使用PATH_INFO模式, 也就是url地址是這樣的 [url=http://localhost/index.php/]http://localhost/index.php/[/url]控制器/方法 /其餘參數
{
if(isset($_SERVER['PATH_INFO']))
{
//$_SERVER['PATH_INFO']URL地址中文件名後的路徑信息, 很差理解, 來看看例子
//好比你如今的URL是 [url=http://www.php100.com/index.php]http://www.php100.com/index.php[/url] 那麼你的$_SERVER['PATH_INFO']就是空的
//可是若是URL是 [url=http://www.php100.com/index.php/abc/123]http://www.php100.com/index.php/abc/123[/url]
//如今的$_SERVER['PATH_INFO']的值將會是 index.php文件名稱後的內容 /abc/123/
$path = trim($_SERVER['PATH_INFO'], '/');
$paths = explode('/', $path);
$control = array_shift($paths);
$action = array_shift($paths);
}
}
//這裏判斷控制器的值是否爲空, 若是是空的使用默認的
$_GET['con'] = !empty($control) ? $control : $C['DEFAULT'];
//和上面同樣
$_GET['act'] = !empty($action) ? $action : $C['DEFAULT_ACTION'];
}
}
?>
--------------------------------------------------------------------------------------------------
welcome.class.php 文件
<?php class Welcome { function Index() { echo '歡迎使用此CMS系統'; } function Run() { echo 'Hello'; } function Show() { echo '方法名show'; } }?>