• php MVC原理


    一直用php的mvc模式,但是一直没深入研究其原理性的东西,今天把最基本的mvc原理模型总结如下:

    1、url访问方式  http://127.0.0.1:8080/ceshi.com/index.php?act=user&op=user

    2、建立如下的目录结构

    3、index.php入口文件

    //路由设置
        $act = $_GET['act'];
        $class = $act.'Control';//获取类名
        $op = $_GET['op'];//获取方法
        
        //自动加载类
        function __autoload($cla){
            if(substr($cla,-7)=="Control"){//control
                $cla = str_replace("Control",'',$cla);
                require 'control/'.$cla.'.php';
            }else{//model
                require 'model/'.$cla.'.php';
            }
        }
        
        require('lib/func.php');//公用方法
        
        $control = new $class();
        $control->$op();

    4、公共方法lib/func.php

    function Model($model = null){
        //$file_name = 'model/'.$model.'Model.php';
        $class_name = $model.'Model';
        
        //require($file_name);
        return new $class_name();//类名
    }
    /**
     * 格式化打印数据
     **/
    function p($v,$k=false){
        echo "<pre>";print_r($v);echo"</pre>";
        $k && exit();
    }

    5、Model基类model/Model.php

    class Model{
        public $mysqli = null;
        public function __construct(){
            $this->mysqli =  new MySQLi("localhost","root","","test");
        }
    }

    6、Model子类(常用类)model/test.model.php

    class testModel extends Model{
        public function dataList(){
            $sql = "select * from article";
            $encode = $this->mysqli->query("set names gb2312");
            $datalist = $this->mysqli->query($sql);
            $listarr = array();
            while($row = $datalist->fetch_assoc()){
                $listarr[] = $row;
            }
            return $listarr;
        }
    }

    7、control  control/user.php

    class userControl{
        public function user(){
            //echo 'user';
            $test_model = Model('test');
            $list = $test_model->dataList();
            include('view/user_list.php');//此处用模板引擎处理
            p($list);
        }
    }

    8、视图文件 view/user.list.php

    $arr = array('a',1,'b');
    foreach($arr as $k=>$v){
        echo $v.'<br />';    
    }

    9、最终结果

    If the copyright belongs to the longfei, please indicate the source!!!
  • 相关阅读:
    两种方法生成随机字符串
    cmd命令总结
    NOI前乱写
    多校模拟9
    字符串 口胡
    HEOI2020游记
    省选模拟104
    省选模拟103
    省选模拟102
    省选模拟101
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/5262996.html
Copyright © 2020-2023  润新知