abstract:<?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 打开执行后实例化框架基础类的调试状态,自动加载类文件如:当用户提供: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 /** *路由、分发类 */ 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"; } }
Correcting teacher:天蓬老师Correction time:2019-05-13 09:17:42
Teacher's summary:通过这个小框架 , 相信你对一个php框架的运行的基本流程有了一个大致的了解...
一个真实商用的php框架, 它的复杂度远高于这个教学框架, 但是底层的原理与架构, 与这个并无本质区别..
掌握 这个小框架的编程思想和思维模式, 对于你以后学习成熟的框架开发, 如laravel, thinkphp,ci, yii等, 非常有好处的, 加油...