PHP架構的打造原理

來源:互聯網
上載者:User
PHP架構的製作原理

index.php 主入口檔案


? define('ISEXIST',true);
? require "init.php";
? $control = new Controller();
? $control -> Run();
?>
---------------------------------------------------------------------------------------------

init.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';
??
?>
----------------------------------------------------------------------------------------------

config.php 檔案


? if(!defined('ISEXIST'))
? ?exit("請從入口檔案運行程式");
? $C = array(
? 'URL_MODE'=>1,//url模式,1為普通模式,2為path_info模式
? 'DEFAULT'=>'welcome',//預設的控制器
? 'DEFAULT_ACTION'=>'index'//預設的方法
? );
?>
-----------------------------------------------------------------------------------------------

controller.class.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控制器不存在
". "請檢查: ".$controlFile."是否存在
");
? }
? 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 檔案

?

? class Welcome
? {
? ?function Index()
? ?{
? ? echo '歡迎使用此CMS系統';
? ?}
? ?function Run()
? ?{
? ? echo 'Hello';
? ?}
? ?
? ?function Show()
? ?{
? ? echo '方法名show';
? ?}
? }

?>

?

轉自:http://blog.sina.com.cn/s/blog_6ec912d80101916t.html

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.