本文實例講述了PHP從零開始打造自己的MVC框架之入口文件實現方法。分享給大家供大家參考,具體如下:
首先來了解一下框架的運行流程:
入口文件 -> 定義常量 -> 引入函數庫 -> 自動加載類 -> 啟動框架 -> 路由解析 -> 加載控制器 -> 返回結果
入口文件index.php:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?php /* 入口文件 1.定義常量 2.加載函數庫 3.啟動框架 */ // 定義當前框架所在的根目錄 define( 'IMOOC' , __DIR__); // 定義框架核心文件所在的目錄 define( 'CORE' , IMOOC. '/core' ); // 項目文件所在目錄 define( 'APP' , IMOOC. '/app' ); // 定義項目調試模式 define( 'DEBUG' , true); // 判斷項目是否處于調試狀態 if (DEBUG) { // 設置報錯級別:顯示所有錯誤 ini_set ( 'display_error' , 'On' ); } else { ini_set ( 'display_error' , 'Off' ); } // 加載函數庫 include CORE. '/common/function.php' ; // 加載框架核心文件 include CORE. '/imooc.php' ; \core\Imooc::run(); |
框架核心目錄里的公共函數function.php:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php /* 輸出對應的變量或者數組 */ function p( $var ){ if ( is_bool ( $var )){ var_dump( $var ); } elseif ( is_null ( $var )) { var_dump(NULL); } else { echo '<pre style="position:relative;z-index:1000;padding:10px;border-radius:5px;background:#f5f5f5;border:1px solid #aaa;font-size:14px;line-height:18px;opacity:0.9;">' .print_r( $var ,true). '</pre>' ; } } |
框架核心文件imooc.php:
1
2
3
4
5
6
7
8
9
|
<?php namespace core; class Imooc { static public function run() { p( 'ok' ); } } |
運行項目,訪問入口文件index.php,瀏覽器如期輸出一個:ok
希望本文所述對大家PHP程序設計有所幫助。
原文鏈接:https://blog.csdn.net/github_26672553/article/details/53860877