サマリー:<?php require 'vendor/autoload.php'; require 'pig/Base.php'; define('ROOT_PATH',__DIR__.'/'); $config=require 'pig/config.php'; $queryStr=$_S
<?php require 'vendor/autoload.php'; require 'pig/Base.php'; define('ROOT_PATH',__DIR__.'/'); $config=require 'pig/config.php'; $queryStr=$_SERVER['REQUEST_URI']; (new \pig\Base($config,$queryStr))->run();
框架入口文件 index.php 打開執(zhí)行后實例化框架基礎類的調試狀態(tài),自動加載類文件如:當用戶提供:php.edu/demo/index/a 后框架自動加載檢測準備new的類生成絕對路徑.
D:\myphp_www\PHPTutorial\WWW\php.edu/pig/Route.php
D:\myphp_www\PHPTutorial\WWW\php.edu/app/admin/controller/Index.php
<?php /** * * 框架基礎類 * 1. 調試模式 * 2. 自動加載 * 3. 啟動框架 */ namespace pig; class Base { protected $config=[]; protected $queryStr=''; public function __construct($config,$queryStr) { $this->config=$config; $this->queryStr=$queryStr; } public function setDebug() { if($this->config['app']['debug']){ error_reporting(E_ALL); ini_set('display_errors','On'); }else{ error_reporting(E_ALL); ini_set('display_errors','off'); ini_set('log_errors','On'); } } public function loader($class) { $path = ROOT_PATH.str_replace('\\','/',$class).'.php'; echo $path.'<br>'; if (!file_exists($path)){ header('Location:/'); } require $path; } public function run() { $this->setDebug(); spl_autoload_register([$this,'loader']); echo(new Route($this->config['route']))->parse($this->queryStr)->dispatch(); } }
<?php /** *路由、分發(fā)類 */ namespace pig; class Route { // protected $route = []; public $route = []; // protected $pathInfo=[]; public $pathInfo=[]; // protected $params=[]; public $params=[]; public function __construct($route) { $this->route=$route; } public function parse($queryStr='') { $queryStr=trim(strtolower($queryStr),'/'); $queryArr=explode('/',$queryStr); $queryArr=array_filter($queryArr); switch (count($queryArr)) { case 0: $this->pathInfo =$this->route; break; case 1: $this->pathInfo['module']=$queryArr[0]; break; case 2: $this->pathInfo['module']=$queryArr[0]; $this->pathInfo['controller']=$queryArr[1]; break; case 3: $this->pathInfo['module']=$queryArr[0]; $this->pathInfo['controller']=$queryArr[1]; $this->pathInfo['action']=$queryArr[2]; break; default: $this->pathInfo['module']=$queryArr[0]; $this->pathInfo['controller']=$queryArr[1]; $this->pathInfo['action']=$queryArr[2]; $arr=array_slice($queryArr,3); for ($i=0;$i<count($arr);$i +=2){ if (isset($arr[$i+1])){ $this->params[$arr[$i]]=$arr[$i+1]; } } break; } return $this; } public function dispatch() { $module=$this->pathInfo['module']; $controller='\app\\'.$module.'\controller\\'.$this->pathInfo['controller']; $action = $this->pathInfo['action']; // new $controller; if(!method_exists($controller,$action)){ $action=$this->route['action']; header('Location:/'); } return call_user_func_array([new $controller,$action],$this->params); } }
<?php /** * Created by PhpStorm. * User: 普通用戶 * Date: 2019/5/13 * */ namespace app\demo\controller; class index { public function a() { echo "demo->index->a"; } }
添削の先生:天蓬老師添削時間:2019-05-13 09:17:42
先生のまとめ:通過這個小框架 , 相信你對一個php框架的運行的基本流程有了一個大致的了解...
一個真實商用的php框架, 它的復雜度遠高于這個教學框架, 但是底層的原理與架構, 與這個并無本質區(qū)別..
掌握 這個小框架的編程思想和思維模式, 對于你以后學習成熟的框架開發(fā), 如laravel, thinkphp,ci, yii等, 非常有好處的, 加油...