从0打造自己的mvc框架

为什么要开发自己的框架?

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';
     }
 }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值