PHP MVC初体验
来源:互联网 发布:ios炫字体 源码 编辑:程序博客网 时间:2024/06/05 11:58
0、唠叨
好久没有更新博客了,这学期上课也没有状态,不知道是临近毕业了,还是因为一些事情影响了心情。总之心态不太对吧,大学里面做的一些项目 好好坏坏也都上线使用了,但是过程我很狼狈,呵呵。深知团队的重要性,文笔不好,口才不好,也就只适合做编程了吧!哈哈。今天突然发现这学期php 我一节课没听过,快要陌生了,就写了一个小例子,没有过多的注释,就算是记录吧!
1、数据库连接类
<?php/** * 连接数据库 * User: qiangzi * Date: 2017/3/22 * Time: 10:31 */class MySqlPDO{ private $dbConfig=array( //数据库配置数组 'db'=>'mysql', 'host'=>'localhost', 'port'=>'3306', 'user'=>'root', 'pass'=>'123456789', 'charset'=>'utf8', 'dbname'=>'', ); private static $instance; private $db; private function __construct($params) { $this->dbConfig=array_merge($this->dbConfig,$params); $this->connect(); } public static function getInstance($params=array()){ if(!self::$instance instanceof self){ self::$instance = new self($params); } return self::$instance; } private function connect(){ try{ $dns="{$this->dbConfig['db']}:host={$this->dbConfig['host']};port={$this->dbConfig['port']};dbname={$this->dbConfig['dbname']};charset={$this->dbConfig['charset']};"; $this->db=new PDO($dns,$this->dbConfig['user'],$this->dbConfig['pass']); $this->db->query("set names {this->dbConfig['charset']}"); }catch(PDOException $e){ die("数据库操作失败:{$e->getMessage()}"); } } public function query($sql){ $res=$this->db->query($sql); if($res===false){ $error=$this->db->errorInfo(); die("数据库操作失败:ERROR{$error[1]}($error[0]):{$error[2]}"); } return $res; } public function fetchRow($sql){ return $this->query($sql)->fetch(PDO::FETCH_ASSOC); } public function fetchAll($sql){ return $this->query($sql)->fetchAll(PDO::FETCH_ASSOC); }}
2、model基类(M)
/** * Created by PhpStorm. * User: qiangzi * Date: 2017/4/5 * Time: 10:56 */class model{ protected $db; public function __construct() { $this->initDB(); } public function initDB(){ $dbConfig=array('user'=>'root','pass'=>'123456','dbname'=>'php'); $this->db=MySqlPDO::getInstance($dbConfig); }}
2.1 model类
<?php/** * Created by PhpStorm. * User: qiangzi * Date: 2017/4/5 * Time: 11:03 */class student extends model{ //获取所有列,调用的MySqlPDO fetchAll方法 public function getAll(){ $data=$this->db->fetchAll('select * from student'); return $data; }}
3、controller(C)
/** * Created by PhpStorm. * User: qiangzi * Date: 2017/4/5 * Time: 11:09 */class studentController{ public function listAction(){ $stu= new student(); $studata= empty($stu->getAll())?array():$stu->getAll();//如果无数据,返回空数据 require'./view/student.html'; }}
4、view(V)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><table> <tr> <th>id</th> <th>姓名</th> <th>性别</th> <th>年龄</th> </tr> <?php foreach($studata as $stu): ?> <tr> <td><?php echo $stu['id']?></td> <td><?php echo $stu['name']?></td> <td><?php echo $stu['gender']?></td> <td><?php echo $stu['age']?></td> </tr> <?php endForeach;?></table></body></html>
5、路由(算是C层,接收返回用户交互)
<?php/** * Created by PhpStorm. * User: qiangzi * Date: 2017/4/5 * Time: 11:28 */require 'model/model.php';//加载modelrequire 'model/student.php';require 'config/MySqlPDO.php';//加载数据连接require 'controller/studentController.php';//加载控制层$controller= new studentController();$controller->listAction();
6、碰到的问题
6.1在视图文件中使用 PHP 替代语法
<?php foreach($studata as $stu): ?> <tr> <td><?php echo $stu['id']?></td> <td><?php echo $stu['name']?></td> <td><?php echo $stu['gender']?></td> <td><?php echo $stu['age']?></td> </tr> <?php endForeach;?>
foreach() 后面是: 结束循环标志是 endForeach
6.2 文件加载规则
先看下项目结构,如下图
如控制层要跳转到页面,我们要加载页面的路径,由于studentController.php 与 student.html 为同级子目录,所以加载 路径就是 ‘./view/student.html’
1 0
- PHP MVC初体验
- Spring3 MVC 初体验
- Spring MVC初体验
- MVC架构初体验
- Spring MVC初体验
- MVC初体验
- Spring MVC初体验
- ASP.NET MVC初体验
- PHP初体验
- PHP初体验
- MVC框架Apache-Tapestry5的初体验
- Spring-MVC 初体验(一)
- Spring MVC 初体验(二)
- Spring MVC 初体验(三)
- Spring MVC初体验之出师不利
- asp.net mvc+jqueryMobile初体验
- Spring MVC初体验之xml配置文件
- php的composer初体验
- 加盐加密
- 升级10.11后使用CocoaPod出现-bash: pod: command not found 解决办法
- 用 canvas 实现 Web 手势解锁
- Android RecycleView出现大间隔问题分析
- Linux 的 core 文件
- PHP MVC初体验
- 如何从链表中删除重复数据
- POJ 2000 Gold Coins G++
- 对上拉电阻的理解
- 使用gem命令时出现的几种错误
- MATLAB R2014b Failed to start pool
- 【python】文本处理:利用NLTK断句
- ReactNative系列之五TextInput
- 程序员的自我修养——3、目标文件里有什么