批改状态:合格
老师批语:
原理:路由解析,从url地址中获取控制器、操作方法
文件路径:20230316\app\admin\controller\User.php
namespace app\admin\controller;require '../app/admin/controller/User.php';class Route{// 路由解析public static function parse(){// array_key_exists 判断数组下标是否存在(键值 索引)// 判断超全局函数$_SERVER是否存在'PATH_INFO'下标if ( array_key_exists('PATH_INFO',$_SERVER ) && ['PATH_INFO'] !== '/'){$pathinfo = array_values(array_filter(explode('/', $_SERVER['PATH_INFO'])));// explode 分割函数// array_filter 返回过滤数组中为空的元素// array_values 获取数组中所有元素的值(或者是说重新索引)// print_r($pathinfo);if(count($pathinfo)>=2){// ucfirst 首字母大写//$controller = array_shift($pathinfo);$controller = __NAMESPACE__ . '\\' . ucfirst(array_shift($pathinfo));$action = array_shift($pathinfo);$params = $pathinfo; //输出 array(0=>peter, 1=>12345);} else {$controller = array_shift($pathinfo);}}// 从url中解析出控制器 操作方法 参数列表return [$controller, $action, $params];}}$res = Route::parse();// call_user_func_array 调用回调函数,并把一个数组参数作为回调函数的参数// 两个参数:callback 被调用的函数,args 要被传入回调函数的数组,得是索引数组echo call_user_func_array([(new $res[0]), $res[1]] , $res[2]);
文件路径:20230316\app\admin\controller\User.php
<?phpnamespace app\admin\controller;class User{public function index($name, $id){return "晚上好,{$name},您的编号为{$id}";}}

USER 下面的一个具体操作方法 index 上index 方法上;
<?phpnamespace core;use PDO;// 被委托的类:查询构造类class Query{protected PDO $db;protected string $table;protected string $field;protected string $limit;// 查询规则 子查询protected array $opts = [];// 构造器public function __construct(PDO $db){$this->db = $db;}// 设置数据表// 返回类型 self 本对象public function table(string $table):self{$this->table = $table;return $this;}// 设置数据查询字段public function field(string $field='*'):self{$this->field = $field;return $this;}// 设置分页,每页显示数据条数public function limit(int $limit=10):self{$this->limit = $limit;$this->opts['limit'] = " LIMIT $limit";return $this;}// 设置分页,计算偏移量public function page(int $num=1):self{$this->opts['offset'] = ' OFFSET ' . ($num-1) * $this->limit;return $this;}// where 条件:数据表查询条件public function where(string $where = ''):self{$this->opts['where'] = " WHERE $where";return $this;}// 排序规则public function order($field, $order = 'DESC'): self{$this->opts['order'] = " ORDER BY $field $order ";return $this;}// 设置查询语句// 返回数组类型public function select(): array{$sql = 'SELECT ' . $this->field . ' FROM ' . $this->table;$sql .= $this->opts['where'] ?? null;$sql .= $this->opts['order'] ?? null;$sql .= $this->opts['limit'] ?? null;$sql .= $this->opts['offset'] ?? null;echo $sql;$stmt = $this->db->prepare($sql);$stmt->execute();return $stmt->fetchAll(PDO::FETCH_ASSOC);}// 更新语句public function update(array $data): int{$str = '';foreach ($data as $k => $v) {$str .= $k . '="' . $v . '",';}// 去掉最后一个逗号// rtrim 移除字符串右侧的空白字符或其他预定义字符$sql = 'UPDATE ' . $this->table . ' SET ' . rtrim($str, ',');$sql .= $this->opts['where'] ?? die('禁止无条件更新');$stmt = $this->db->prepare($sql);$stmt->execute();// 大于0表示更新成功,需要有返回值才能进行下一步工作return $stmt->rowCount();}}
Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号