** 實(shí)際上框架的目的就是為了讓前后端分離,代碼更清晰,便于維護(hù),各干各的事情.一目了然.**
- 新建框架根目錄 dream
- 在根目錄下建立單一入口文件 index.php
<?php
define('DREAM', __DIR__);//定義根目錄常量
define('APP', DREAM .'/App/');//定義應(yīng)用目錄
define('CORE' , DREAM ,'/Core/');//定義核心文件目錄
define('DEBUG' , true);//定義調(diào)試模式
//判斷是否開(kāi)啟調(diào)試
if(DEBUG){
ini_set('display_errors' , 'On');
}else{
ini_set('display_errors' , 'Off');
}
//引入公共函數(shù)類庫(kù)
require CORE . 'Common/function.php';
//引入框架核心文件
require CORE . 'dream.php';
//注冊(cè)自動(dòng)加載類
spl_autoload_register('\Core\dream::_autoload');
//啟動(dòng)框架
Core\dream::run();
- 在根目錄新建 App文件夾 和 Core文件夾
- 在Core文件夾下新建 Common文件夾
- 在Common 文件夾下 新建 function.php
<?php
配置公共方法
function p($var) //打印函數(shù)
{
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>";
}
}
- 在Core 文件 下 新建框架核心文件 dream.php
<?php
namespace Core;//定義命名空間
class dream
{
static public $classFile = [];
//框架啟動(dòng)方法
static public function run()
{
}
//自動(dòng)加載類 當(dāng)我們new 一個(gè)不存在的類 時(shí) 自動(dòng)調(diào)用該方法
static public function _autoload($class)
{
//先處理 類名 的反斜線為正斜線
$class = str_replace('\\', '/', $class);
//然后再拼接文件路勁
$file = DREAM . '/' . $class . '.php';
//判斷文件是否存在
if(isset($classFile[$class])){
return true;
} else {
require $file;
$classFile[$class] = $class;
}
}
- 在Core文件 下 新建路由文件 route.php
<?php
namespace Core;
class route
{
public $ctrl;
public $action;
public function __construct()
{
//判斷路勁是否存在 且 路勁不能等于 '/'
if(isset($_SERVER['REQUEST_URL']) && $_SERVER['REQUEST_URL'] != '/'){
//接收并處理路徑
$path = $_SERVER['REQUEST_URL'];
//去除兩邊的'/',并分割
$patharr = explode('/', trim($path, '/'));
//如果存在下標(biāo)為 0 的就存儲(chǔ)為控制器名
if(isset($patharr[0])){
$this->ctrl = $patharr[0];
}
//釋放 為后面截取參數(shù)做準(zhǔn)備
unset($patharr[0]);
//下標(biāo)為 1 的如果存在就存儲(chǔ)為方法名 并釋放 否則就默認(rèn)為index操作
if(isset($patharr[1])){
$this->action = $patharr[1];
unset($patharr[1]);
} else {
$this->action = 'index';
}
//統(tǒng)計(jì)$patharr 的數(shù)量
$count = count($patharr) + 2;
$i = 2;
while($i < $count){
//判斷傳的參數(shù)是否都存在 然后獲取參數(shù) 否則不做動(dòng)作
if(isset($patharr[$i] + 1)){
$_GET[$patharr[$i]] = $patharr[$i + 1];
}
$i = $i +2;
}
} else {
$this->action = 'index';
$this->ctrl = 'index';
}
}
}
然后再 dream.php 核心文件中加載 控制和方法
<?php
namespace Core;
class keep
{
static public $classMap = [];
public $assign;
static public function run()
{
$route =new \Core\route();
$ctrlClass = ucfirst($route->ctrl);
$action = $route->action;
$ctrlFile = APP.'Controller/'.$ctrlClass.'Controller.class.php';
$ctrlClass = '\\'.MODULE.'\\Controller\\'.$ctrlClass.'Controller';
if (is_file($ctrlFile)) {
require $ctrlFile;
$ctrl = new $ctrlClass;
$ctrl->$action();
} else {
throw new \Exception('找不到控制器',$ctrlClass);
}
}
static public function _autoload($class)
{
$class = str_replace('\\','/',$class);
$file = KEEP.'/'.$class.'.php';
if (isset(self::$classMap[$class])) {
return true;
}else{
if (is_file($file)) {
require $file;
self::$classMap[$class] = $class;
}else{
return false;
}
}
}
//將控制器里發(fā)送的數(shù)據(jù) 接收 并存儲(chǔ)
public function assign($name,$value)
{
$this->assign[$name] = $value;
}
//加載模板文件
public function display($file)
{
//對(duì)接收到了文件名 拼接處理
$file = APP.'View/'.$file;
//如果存在 就引入
if (is_file($file)) {
//這里extract 是php 的一個(gè)函數(shù) 作用是將 數(shù)據(jù)打散 在模板使用
extract($this->assign);
require $file;
}
}
}
- 并在App 文件夾下 新建 Controller 文件夾.
- 在Controller 文件夾下 新建IndexController.class.php //這里為了防止命名沖突導(dǎo)致加載錯(cuò)誤 所以名稱這樣建
- 在Core 文件夾下 新建 Lib 文件夾 在建立 model.php//連接數(shù)據(jù)庫(kù)
<?php
namespace Core\Lib;
class model extends \PDO //繼承PDO
{
public function __construct()
{
$dsn = 'mysql:host=localhost;dbname=test';//主機(jī)名 和庫(kù)名
$username = 'root';//用戶名
$passwd = 'root';//密碼
try {
parent::__construct($dsn, $username, $passwd);
} catch (\PDOException $e) {
p($e->getMessage());//失敗拋出異常
}
}
}
- 在Controller 下的IndexController.class.php 文件 進(jìn)行數(shù)據(jù) 發(fā)送模板 加載模板
- $this->assign 方法 和$this->display 方法 在核心文件 dream.php 中定義
<?php
namespace App\Controller;
use Core\Lib\model;
class IndexController extends \Core\keep
{
public function index()
{
$model = new \Core\Lib\model();
$sql = "SELECT * from qiduo_market";
$res = $model->query($sql);
$data = $res->fetchAll();
// $data = 'Hello Wrold';
$this->assign('data',$data);
$this->display('index.html');
}
}
** 一套流程就是這樣的,框架的作用是為了提高重用性,可維護(hù)性和便捷的操作.但是由于框架眾多,功能齊全,但是自己又用不到那么多,占用資源.所以有必要試試做一個(gè)自己的框架. **