设计模式简介
设计模式:代表了最佳的代码实践,通常被有经验的面向对象的软件开发人语言所采用。
使用设计模式是为了重用代码、让代码更容易被他人理解、保证代码的可靠性。
常用的设计模式有23种之多
MVC基础
MVC是模型(Model)-视图(View)-控制器(Controller)的缩写
MVC是一种将业务逻辑、数据、界面显示分离代码的阻止方法。
Model(模型):表示应用程序的核心(比如数据库记录列表)
View(视图):显示数据(数据库记录html、css)
Controller(控制器):(控制器)处理输入(写入数据库记录)
MVC工作原理
单一入口工作原理
单一入口通常指一个项目或者应用具有一个统一的(注意:并不一定是唯一的)入口文件,也就是说项目的所有功能操作都是通过这个入口文件进行的,并且往往入口文件是第一步被执行的。
工作原理:先找到控制器文件 -> 在控制器文件中的控制器方法
1 |
echo $_SERVER['PATH_INFO']; |
EXAMPLE:用户注册和用户展示
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php header('content-type:text/html;charset=utf-8'); // echo '我是入口文件,所有的请求都由我转化'; // 初始化的时候没有pathinfo所以给@否则会warning @$pathinfo = substr($_SERVER['PATH_INFO'],1); $array = explode('/', $pathinfo); @list($controller,$action) = $array; if($controller == ''){ $controller = 'IndexController'; } if($action == ''){ $action = 'index'; } // 包含相关的控制文件 require_once 'controller/'.$controller.'.php'; // 实例化控制器(路由) $class = new $controller(); // print_r($class); // 调用方法 $class -> $action(); |
IndexController.php
1 2 3 4 5 6 |
<?php class IndexController{ public function index(){ require_once 'view/index.html'; } } |
Controller.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php require_once 'model/Database.php'; class UserController{ // 呈现用户注册页面 public function create(){ require_once 'view/adduser.html'; } // 获取表单数据并且写入数据库 public function add(){ $db = new Database(); $db -> insert($_POST); } // 获取所有用户数据,并且呈现HTML页面 public function list(){ $db = new Database(); $sql = 'SELECT id,username,sex FROM users'; $rowset = $db -> query($sql); // print_r($rowset); require_once 'view/listuser.html'; } } |
Database.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php //Model类 class Database{ protected $pdo; public function __construct(){ $this->pdo = new PDO('mysql:host=localhost:8889;dbname=mooc','root','root'); } /* * $bind的形态['username'=>?,'password'=>?,'sex'=>?] * */ function insert($bind){ $sql = 'insert users(username,password,sex) values(?,?,?)'; $stmt=$this->pdo->prepare($sql); $stmt->bindValue(1,$bind['username']); $stmt->bindValue(2,$bind['password']); $stmt->bindValue(3,$bind['sex']); $stmt->execute(); } /** * @param string $sql,SQL查询语句 * */ function query($sql){ $stmt = $this->pdo->query($sql); $rowset = $stmt->fetchAll(PDO::FETCH_ASSOC); return $rowset; } } |
adduser.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> #table{ width: 600px; margin:0 auto; border-collapse: collapse; border-spacing: 0; } #table td{ padding: 10px; border: 1px solid #333; } </style> </head> <body> <h1>用户注册</h1> <form action="/MVC/index.php/UserController/add" method="post"> <table id="table"> <tr> <td width="100">用户名</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="password"></td> </tr> <tr> <td>性别</td> <td> 男<input type="radio" name="sex" value="0"> 女<input type="radio" name="sex" value="1" checked> </td> </tr> <tr> <td> </td> <td><input type="submit" value="免费注册"></td> </tr> </table> </form> </body> </html> |
index.html
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>hello world</h1> <p><a href="/MVC/index.php/UserController/create">用户注册</a></p> <p><a href="/MVC/index.php/UserController/list">用户列表</a></p> </body> </html> |
listuser.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> #table{ width: 600px; margin:0 auto; border-collapse: collapse; border-spacing: 0; } #table td{ padding: 10px; border: 1px solid #333; } </style> </head> <body> <h1 align="center">用户列表</h1> <table id="table"> <tr> <td>序号</td> <td>用户名</td> <td>性别</td> </tr> <tr> <?php foreach($rowset as $row){?> <td><?php echo $row['id']?></td> <td><?php echo $row['username']?></td> <td><?php echo $row['sex']?'女':'男'?></td> </tr> <?php }?> </table> </body> </html> |