批改状态:合格
老师批语:还有写上php作业, 加油了
写一个依赖注入的案例, 内容自定义
依赖注入实际和前端应用外部的css或是js文件一样,目的引用外部代码,优化代码结构,减少写重复代码。
实例引用写好查询数据库代码文件,在新的文件查询数据库文件。
外部引入的查询数据库文件Query.php
<?php
namespace _php;
// 数据库查询类
class Query
{
public $pdo = null;
public $table;
public $field = '*';
public $where;
public $limit;
// 构造方法
public function __construct($pdo,$user='root',$password='cqx07231950')
{
// 错误示范
// $this->pdo = $pdo;
//// return new \PDO($pdo,$user,$password);
$this->pdo = new \PDO($pdo,$user,$password);
}
// 设置数据表名称
public function table($tableName)
{
$this->table = $tableName;
//返回当前类实例, 用来链式调用后面的其它方法
return $this;
}
// 设置数据表字段
public function field($fields = '*')
{
$this->field = empty($fields) ? '*' : $fields;
return $this;
}
// 设置查询条件
public function where($where = '')
{
$this->where = empty($where) ? $where : ' WHERE '. $where;
return $this;
}
// 设置显示数量
public function limit($limit)
{
$this->limit = empty($limit) ? $limit : ' LIMIT '. $limit;
return $this;
}
// 生成SQL语句
public function select()
{
// SELECT * FROM table WHERE **** LIMIT n
// 拼装SQL
$sql = 'SELECT '
. $this->field // 字段列表
. ' FROM '
. $this->table // 数据表
. $this->where // 条件
. $this->limit; // 显示数量
// 预处理
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
// die($stmt->debugDumpParams()); // 查看生成的sql
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
}<?php
namespace _php;
require 'Query.php';
$db = new Query('mysql:host=127.0.0.1;dbname=chenqingxuan','root','cqx07231950');
$staffs = $db->table('staff')
->field('staff_id,name,position,mobile')
->where('staff_id > 2')
->limit(10)
->select();
// 遍历
foreach ($staffs as $staff) {
print_r($staff); echo '<br>';
}点击 "运行实例" 按钮查看在线实例

2. 写一个mvc应用的案例, 内容自定义
在1的基础上 将model文件和view文件引入1.php.在1.php中取得数据并渲染数据
C文件 1.php
<?php
namespace _php;
require 'Query.php';
$db = new Query('mysql:host=127.0.0.1;dbname=chenqingxuan','root','cqx07231950');
$staffs = $db->table('staff')
->field('staff_id,name,position,mobile')
->where('staff_id > 2')
->limit(10)
->select();
require 'dy.php';
//print_r($staffs);
// 遍历
//foreach ($staffs as $staff) {
// print_r($staff); echo '<br>';
//}<?php
namespace _php;
// 数据库查询类
class Query
{
public $pdo = null;
public $table;
public $field = '*';
public $where;
public $limit;
// 构造方法
public function __construct($pdo,$user='root',$password='cqx07231950')
{
// 错误示范
// $this->pdo = $pdo;
//// return new \PDO($pdo,$user,$password);
$this->pdo = new \PDO($pdo,$user,$password);
}
// 设置数据表名称
public function table($tableName)
{
$this->table = $tableName;
//返回当前类实例, 用来链式调用后面的其它方法
return $this;
}
// 设置数据表字段
public function field($fields = '*')
{
$this->field = empty($fields) ? '*' : $fields;
return $this;
}
// 设置查询条件
public function where($where = '')
{
$this->where = empty($where) ? $where : ' WHERE '. $where;
return $this;
}
// 设置显示数量
public function limit($limit)
{
$this->limit = empty($limit) ? $limit : ' LIMIT '. $limit;
return $this;
}
// 生成SQL语句
public function select()
{
// SELECT * FROM table WHERE **** LIMIT n
// 拼装SQL
$sql = 'SELECT '
. $this->field // 字段列表
. ' FROM '
. $this->table // 数据表
. $this->where // 条件
. $this->limit; // 显示数量
// 预处理
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
// die($stmt->debugDumpParams()); // 查看生成的sql
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
}<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>PHP中文网</title>
<link href="static/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<script src="../static/js/jquery.js"></script>
<div class="container" style="margin-top:20px">
<div class="bg-light lter b-b wrapper-md">
<h1 class="m-n font-thin h3">数据库读取管理</h1>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">姓名</th>
<th scope="col">手机号</th>
<th scope="col">职位</th>
</tr>
</thead>
<tbody>
<?php
// print_r($staffs);
if(isset($staffs)){
foreach ($staffs as $v){
?>
<tr>
<th scope="row"><?php echo $v['staff_id'];?></th>
<td><?php echo $v['name'];?></td>
<td><?php echo $v['mobile'];?></td>
<td><?php echo $v['position'];?></td>
</tr>
<?php }}?>
</tbody>
</table>
</div>
</body>实际效果:

有没有什么方法,允许在控制器文件(1、php)开头处和M文件一样在开头引入?
3. 写一个简单的路由, 理解路由的原理与目标
/*路由的实现原理: 用户通过制定的URL范式对后台进行访问,URL路由处理类进行处理后,
转发到逻辑处理类逻辑处理类将请求返回给用户*/
/*路由核心
1、对用户请求的URL进行解析处理
2、获取用户请求的类、方法已经Query参数等
3、将请求转发给逻辑处理类处理*/
总结:路由获取 打印$_SERVER中获取 ,修改$_SERVER中数组参数并将其传入其他逻辑处理类暂时不理解 (后续学习)
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号