├── aa.txt └── mvc ├── static └── pengmama.jpg ├── models └── Joke.class.php ├── config └── web.php ├── demo.php ├── views ├── index │ ├── index.html │ └── index.php └── index.php ├── controllers ├── Demo.class.php └── Index.class.php ├── runtime ├── 97aa98c1195d18ee305e7ebfb52e4398.php ├── b96c94a7e6dac798252b100295f08c64.php └── dadcdbcd84856733d0e9225e352e6497.php ├── libs ├── Configure.class.php ├── AutoLoader.class.php ├── Controller.class.php ├── View.class.php ├── Model.class.php └── Router.class.php └── index.php /aa.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mvc/static/pengmama.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liajie/mvc/HEAD/mvc/static/pengmama.jpg -------------------------------------------------------------------------------- /mvc/models/Joke.class.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'host'=>'127.0.0.1', 7 | 'username'=>'root', 8 | 'password'=>'root', 9 | 'dbname'=>'demo', 10 | 'tbPrefix'=>'', 11 | 'pConnect'=>0, 12 | ], 13 | 14 | 15 | 16 | ); -------------------------------------------------------------------------------- /mvc/demo.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
ID昵称发布时间
-------------------------------------------------------------------------------- /mvc/views/index/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | {foreach($data as $row):} 9 | 10 | 11 | 12 | 13 | 14 | {endforeach} 15 |
ID昵称发布时间
{$row['id']}{$row['username']}{$row['addtime']}
-------------------------------------------------------------------------------- /mvc/controllers/Demo.class.php: -------------------------------------------------------------------------------- 1 | 3 | 4 | ID 5 | 昵称 6 | 发布时间 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /mvc/views/index/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
ID昵称发布时间
-------------------------------------------------------------------------------- /mvc/runtime/97aa98c1195d18ee305e7ebfb52e4398.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
ID昵称发布时间
-------------------------------------------------------------------------------- /mvc/runtime/b96c94a7e6dac798252b100295f08c64.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
ID昵称发布时间
-------------------------------------------------------------------------------- /mvc/runtime/dadcdbcd84856733d0e9225e352e6497.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
ID昵称发布时间
-------------------------------------------------------------------------------- /mvc/libs/Configure.class.php: -------------------------------------------------------------------------------- 1 | getMessage(); 22 | 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /mvc/libs/Controller.class.php: -------------------------------------------------------------------------------- 1 | viewObject = new View; 10 | } 11 | public function display($fileName=''){ 12 | // 获取得到模板路径 13 | if(empty($fileName)){ 14 | 15 | $c = Router::getInstance()->controller; 16 | $a = Router::getInstance()->action; 17 | 18 | $fileName = VIEWS.$c.DS.$a.'.html'; 19 | } 20 | 21 | $this->viewObject->display($fileName); 22 | } 23 | 24 | public function assign($data){ 25 | $this->viewObject->assign($data); 26 | } 27 | 28 | 29 | 30 | } -------------------------------------------------------------------------------- /mvc/index.php: -------------------------------------------------------------------------------- 1 | getCon(); 18 | $action = $router->getAc(); 19 | if($obj = new $controller){ 20 | $obj->$action(); 21 | }else{ 22 | throw(new Exception('对不起,我们这里没有你说的这个mm')); 23 | } 24 | }else{ 25 | echo "MVC文件加载错误!"; 26 | } -------------------------------------------------------------------------------- /mvc/controllers/Index.class.php: -------------------------------------------------------------------------------- 1 | select("select * from joke"); 15 | // var_dump($data);die; 16 | $this->assign(['data'=>$data]); 17 | $this->display(); 18 | // 渲染模板引擎 19 | // $data=$model->insert("insert into joke VALUES (null,'小韩','2017-09-28 20:32:58')"); 20 | // var_dump($data);die; 21 | // $data=$model->delete("delete from joke WHERE id='5'"); 22 | // var_dump($data);die; 23 | // $data=$model->save("update joke set username='小杨' WHERE id='4'"); 24 | // var_dump($data);die; 25 | 26 | } 27 | 28 | // public function insert(){ 29 | // $model=new Joke(); 30 | // $data=$model->insert("insert into joke VALUES (null,'小韩','2017-09-28 20:32:58')"); 31 | // 32 | // } 33 | 34 | 35 | } -------------------------------------------------------------------------------- /mvc/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); 32 | $filename = RUNTIME.$filename.'.php'; 33 | if(!file_exists($filename)){ 34 | 35 | // 解析文件 36 | // 其他规则放到前面 37 | $content = preg_replace('#\{foreach#', '', $content); 39 | $content = preg_replace('#\{#iU','', $content); 41 | 42 | file_put_contents($filename,$content); 43 | } 44 | return $filename; 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /mvc/libs/Model.class.php: -------------------------------------------------------------------------------- 1 | $v){ 19 | $this->$key = $v; 20 | } 21 | $dns = "mysql:host={$this->host};dbname={$this->dbname};charset=utf8"; 22 | try{ 23 | $this->connect = new \PDO($dns,$this->username,$this->password); 24 | }catch (PDOException $e) { 25 | echo 'Connection failed: ' . $e->getMessage(); 26 | } 27 | } 28 | 29 | public function insert($sql){ 30 | return $this->connect->exec($sql); 31 | } 32 | 33 | public function save($sql){ 34 | return $this->connect->exec($sql); 35 | 36 | } 37 | 38 | public function delete($sql){ 39 | return $this->connect->exec($sql); 40 | } 41 | 42 | public function select($sql){ 43 | $stm = $this->connect->query($sql); 44 | return $stm->fetchAll(\PDO::FETCH_ASSOC); 45 | } 46 | 47 | public function getTableName(){ 48 | return $this->tableName; 49 | } 50 | 51 | public function setTableName($tableName){ 52 | 53 | $this->tableName = $tableName; 54 | } 55 | 56 | } -------------------------------------------------------------------------------- /mvc/libs/Router.class.php: -------------------------------------------------------------------------------- 1 | controller = $arr_path[1]; 20 | } 21 | if(isset($arr_path[2]) && !empty($arr_path[2]) ) { 22 | $this->action = $arr_path[2]; 23 | } 24 | // 参数怎么获取 25 | for($i=3;$icontroller = $_GET['c']; 43 | } 44 | return $this->controllerNamespace.$this->controller; 45 | } 46 | 47 | public function getAc(){ 48 | if(isset($_GET['c_a']) && !empty($_GET['c_a'])){ 49 | $this->action = $_GET['c_a']; 50 | } 51 | return $this->action; 52 | } 53 | 54 | } --------------------------------------------------------------------------------