└── mvcs ├── static └── pengmama.jpg ├── models └── Joke.class.php ├── controllers ├── Demo.class.php └── Index.class.php ├── config └── web.php ├── views ├── index │ ├── index.html │ └── index.php └── index.php ├── demo.php ├── runtime ├── 15902358efdaa9d2be361e871ed88ef7.php └── 4784a17cfa2e242c4a6f692ec7c5878f.php ├── libs ├── Configure.class.php ├── AutoLoader.class.php ├── Controller.class.php ├── View.class.php ├── Model.class.php └── Router.class.php └── index.php /mvcs/static/pengmama.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sukangshen/mvc/HEAD/mvcs/static/pengmama.jpg -------------------------------------------------------------------------------- /mvcs/models/Joke.class.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'host'=>'127.0.0.1', 7 | 'username'=>'root', 8 | 'password'=>'root', 9 | 'dbname'=>'test', 10 | 'tbPrefix'=>'', 11 | 'pConnect'=>0, 12 | ], 13 | 14 | 15 | 16 | ); -------------------------------------------------------------------------------- /mvcs/views/index/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | {foreach ($data as $k=>$v):} 9 | {$v['name']} 10 | {$v['age']} 11 | {endforeach} 12 | 13 | 14 | -------------------------------------------------------------------------------- /mvcs/demo.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
ID昵称发布时间
-------------------------------------------------------------------------------- /mvcs/runtime/15902358efdaa9d2be361e871ed88ef7.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | $v):?> 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /mvcs/runtime/4784a17cfa2e242c4a6f692ec7c5878f.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | $v):?> 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /mvcs/views/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
ID昵称发布时间
-------------------------------------------------------------------------------- /mvcs/views/index/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
ID昵称发布时间
-------------------------------------------------------------------------------- /mvcs/libs/Configure.class.php: -------------------------------------------------------------------------------- 1 | '; 14 | try{ 15 | if(file_exists($className)){ 16 | include $className; 17 | }else{ 18 | throw(new Exception('对不起,我们这里没有你说的这个mm')); 19 | //exit; 20 | } 21 | }catch(Exception $e){ 22 | echo $e->getMessage(); 23 | 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /mvcs/libs/Controller.class.php: -------------------------------------------------------------------------------- 1 | viewObject = new View; 10 | } 11 | public function display($fileName=''){ 12 | // 获取得到模板路径 13 | $c = Router::getInstance()->controller; //获取控制器 14 | $a = Router::getInstance()->action; //获取方法 15 | 16 | if(empty($fileName)){ //是否有默认文件 17 | 18 | 19 | $fileName = VIEWS.$c.DS.$a.'.html'; //默认当前控制器文件夹下方法.html 20 | }else{ 21 | $fileName = VIEWS.$c.DS.$fileName.'.html'; 22 | } 23 | 24 | $this->viewObject->display($fileName); //文件百分百存在 25 | } 26 | 27 | public function assign($data){ 28 | $this->viewObject->assign($data); 29 | } 30 | 31 | 32 | 33 | } -------------------------------------------------------------------------------- /mvcs/controllers/Index.class.php: -------------------------------------------------------------------------------- 1 | "; 12 | print_r($_GET);*/ 13 | 14 | 15 | //$model = new Joke(); 16 | //$model->insert('insert into user set username=111'); 17 | //$model->insert('insert into user(username) value("fff")'); 18 | 19 | // $data = $model->select("select * from joke"); 20 | //var_dump($data); 21 | $data = array( 22 | array('name'=>'ff','age'=>'18'), 23 | ); 24 | $this->assign(['data'=>$data]); 25 | $this->display(); 26 | // 渲染模板引擎 27 | } 28 | 29 | public function demo(){ 30 | echo "demo"; 31 | } 32 | 33 | 34 | } -------------------------------------------------------------------------------- /mvcs/index.php: -------------------------------------------------------------------------------- 1 | getCon(); //获取控制器 17 | // print_r($controller)."
";die(); 18 | $action = $router->getAc(); //获取方法 19 | if($obj = new $controller){ 20 | $obj->$action(); 21 | }else{ 22 | throw(new Exception('对不起,我们这里没有你说的这个控制器')); 23 | } 24 | 25 | }else{ 26 | echo "MVC文件加载错误!"; 27 | } -------------------------------------------------------------------------------- /mvcs/libs/View.class.php: -------------------------------------------------------------------------------- 1 | data = $item; 15 | 16 | } 17 | 18 | 19 | public function display($templateFile=''){ 20 | $data = $this->data; 21 | extract($data); //打散函数 22 | // 找到哪个控制器方法 23 | // 将文件包含进来 24 | $fileContent = file_get_contents($templateFile); 25 | $file = $this->parseTemplate($fileContent); 26 | include $file; 27 | } 28 | 29 | 30 | public function parseTemplate($content){ 31 | $filename = md5($content); //文件里面的内容生成MD5 散列值 保证缓存文件唯一 32 | $filename = RUNTIME.$filename.'.php'; 33 | if(!file_exists($filename)){ 34 | 35 | // 解析文件 36 | // 其他规则放到前面 37 | $content = preg_replace('#\{foreach#', '', $content); //替换foreach尾 39 | $content = preg_replace('#\{#iU','', $content); //大括号替换为php尾解析 41 | 42 | file_put_contents($filename,$content); 43 | } 44 | return $filename; 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /mvcs/libs/Model.class.php: -------------------------------------------------------------------------------- 1 | $v){ 20 | $this->$key = $v; 21 | } 22 | $dns = "mysql:host={$this->host};dbname={$this->dbname};charset=utf8"; 23 | try{ 24 | $this->connect = new \PDO($dns,$this->username,$this->password); 25 | }catch (PDOException $e) { 26 | echo 'Connection failed: ' . $e->getMessage(); 27 | } 28 | } 29 | 30 | public function insert($sql){ 31 | return $this->connect->exec($sql); 32 | } 33 | 34 | public function save($sql){ 35 | return $this->connect->exec($sql); 36 | 37 | } 38 | 39 | public function delete($sql){ 40 | return $this->connect->exec($sql); 41 | } 42 | 43 | public function select($sql){ 44 | $stm = $this->connect->query($sql); 45 | return $stm->fetchAll(\PDO::FETCH_ASSOC); 46 | } 47 | 48 | public function getTableName(){ 49 | return $this->tableName; 50 | } 51 | 52 | public function setTableName($tableName){ 53 | 54 | $this->tableName = $tableName; 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /mvcs/libs/Router.class.php: -------------------------------------------------------------------------------- 1 | controller = $arr_path[1]; 21 | } 22 | if(isset($arr_path[2]) && !empty($arr_path[2]) ) {//index.php 后面存在方法名称 为下标为二的参数 23 | $this->action = $arr_path[2]; 24 | } 25 | // 参数怎么获取 26 | for($i=3;$icontroller = $_GET['c']; 44 | } 45 | return $this->controllerNamespace.$this->controller; // 返回 控制器的位置 是可以实例化的; 46 | } 47 | 48 | public function getAc(){ //获取方法 49 | if(isset($_GET['c_a']) && !empty($_GET['c_a'])){ 50 | $this->action = $_GET['c_a']; 51 | } 52 | return $this->action; 53 | } 54 | 55 | } --------------------------------------------------------------------------------