PHP 独自のフレームワーク 2.0 がコントローラーをロードして実行します (リファクタリング パート 4)

目次

 1. コントローラーをロードして実行します

 2. 管理モジュールとインデックスモジュールコントローラを作成します

3. コントローラーファイルを自動的にロードする

4. コントローラーでメソッドを実行します

5. 実行中のモジュールのコントローラーメソッド


 

 1. コントローラーをロードして実行します

 2. 管理モジュールとインデックスモジュールコントローラを作成します
<?php
namespace app\admin\controller;
class index{
    public function index(){
        echo "运行admin模块";
    }
}
<?php
namespace app\index\controller;
class index{
    public function index(){
      echo "运行index模块";
    }
}
3. コントローラーファイルを自動的にロードする
<?php
namespace fm;
class autoload{
    public static  function register(){
        spl_autoload_register(array(__CLASS__,'_autoload'),true, true);
    }
    public static function _autoload($className){
        $path=dirname(dirname(str_replace('\\','//',__FILE__)));
        if (!(strpos($className, 'fm') === 0)) {
            //加载根目录类
            $path=dirname($path);
        }
        $file=$path.'/'.$className.'.php';
        if(is_file($file)){
            include $file;
        }

    }

}
4. コントローラーでメソッドを実行します
<?php
namespace fm;
class core extends Di{
    protected $corePath;//核心目录路径
    protected $rootPath;//项目根目录
    public function __construct()
    {

    }
    public  function run(){
        $this->_int();//初始化常量
        $this->_run();//运行
    }

    //运行控制器中方法
    public static function _run(){
        $m=strtolower(isset($_GET['m'])?$_GET['m']:'index');
        $c=strtolower(isset($_GET['c'])?$_GET['c']:'index');
        $a=strtolower(isset($_GET['a'])?$_GET['a']:'index');
        $c="app\\".$m."\\controller\\".$c;
       if(!class_exists($c)){
            die("控制器".$c."不存在");
        }
        $obj=new $c();

        if(!method_exists($obj,$a)){
            die("控制器".$c."下".$a."方法不存在");
        }
        $obj->$a();
    }

     public function _int(){

         //获取框架核心路径 都替换/以便兼容linux
         $path=str_replace("\\","/",__FILE__);
         //定义常量
         $this->corePath=dirname(dirname($path));
         $this->rootPath=dirname($this->corePath);
         static::setInstance($this);
         $this->bandInstance('core', $this);

     }

}
5. 実行中のモジュールのコントローラーメソッド

localhost/frame/public/index.php?m=admin&c=index&a=index

localhost/frame/public/index.php?m=index&c=index&a=index

おすすめ

転載: blog.csdn.net/weixin_39934453/article/details/132880289