为什么要开发自己的框架?
1.为了更快的开发效率
2.为了更高的运行效率
3.为了更好的证明自己
框架运行流程:
下面我们来进行mvc框架的开发
2-1首先要先创建一个入口文件:
<?php
define('MVC',realpath('./')); //当前框架的根目录
define('CORE',MVC.'/core'); //框架核心文件所处的目录
define('APP',MVC.'/app'); //项目文件(控制器,视图,模型)所处的目录
define('MODULE','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'; //加载框架的核心文件
spl_autoload_register('\core\imooc::load'); //new一个类的时候 如果不存在 会用这个方法
\core\imooc::run(); //调用imooc方法
2-2类自动加载:
static public function load($class)
{
//自动加载类库
if (isset($classMap[$class])) {
return true;
} else {
$class = str_replace('\\', '/', $class); //把\替换为/ 注意 必须是两个\\因为一个\是转移符号
$file=MVC . '/' . $class . '.php';
if (is_file($file)) { //判断文件是否存在
include $file;
self::$classMap[$class] = $class;
} else {
return false;
}
}
}
2-3路由类:
<?php
namespace core\lib;
class route{
public $ctrl;
public $action;
public function __construct(){
/*
* 1.隐藏index.php
* 2.获取URL 参数部分
* 3.返回对应控制器
* */
if(isset($_SERVER["REQUEST_URI"]) && $_SERVER["REQUEST_URI"] != '/'){
$path=$_SERVER["REQUEST_URI"];
$patharr=explode('/',trim($path,'/'));
if(isset($patharr[0])){
$this->ctrl=$patharr[0];
}
unset($patharr[0]);
if(isset($patharr[1])){
$this->action=$patharr[1];
unset($patharr[1]);
}else{
$this->action='index';
}
//url多余部分转为 GET
$count=count($patharr)+2;
$i=2;
while($i<$count){
if(isset($patharr[$i+1])){
$_GET[$patharr[$i]]=$patharr[$i+1];
}
$i=$i+2;
}
unset($_GET['url']);
// p($_GET);
}else{
$this->ctrl='index';
$this->action='index';
}
}
}