路由原理与实现
主流路由解决方案:pathinfo
<?php
require __DIR__ . '/../helper.php';
//路由
//!方案一,主流路由解决方案:pathinfo
$url = 'http://php.edu/0507/router/demo2.php?c=user&a=hello&id=20&name=liyufeng';
p(pathinfo($url));
//显示如下:
// Array
// (
// [dirname] => http://php.edu/0507/router
// [basename] => demo2.php?c=user&a=hello&id=20&name=liyufeng
// [extension] => php?c=user&a=hello&id=20&name=liyufeng
// [filename] => demo2
// )
//!获取查询字符串:键值对:c=user&a=hello
$url = 'http://php.edu/0507/router/demo2.php/one/two/three?c=user&a=hello';
p(pathinfo($url));
// $_SERVER['PATH_INFO']
//获取参数:/one/two/three
// p($_SERVER['PATH_INFO']);
//单入口:index.php?m=模块,例如前台home,后台admin
//模块/控制器/方法
//index.php/module/controller/action
//多入口
// 前台:index.php,不需要模块,/controller/action
// 后台:admin.php,不需要模块,/controller/action
$url = 'http://php.edu/0507/router/demo2.php/admin/user/index';
p($_SERVER['PATH_INFO']);
//explode:把“/admin/user/index”转为数组
p(explode('/',$_SERVER['PATH_INFO']));
//第一种方法:array_filter过滤空数组
// p(array_filter(explode('/',$_SERVER['PATH_INFO'])));
//第一种方法:trim
// p(explode('/', trim($_SERVER['PATH_INFO'],'/')));
$request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
//数组解构:将模块、控制器、方法都解析出来
[$module,$controller,$action] = $request;
//参数解析格式
//按顺序传参
//id = 1
//name = admin
$url = 'http://php.edu/0507/router/demo2.php/admin/user/index/1/admin';
//按键名进行传参
//id = 1
//name = admin
$url = 'http://php.edu/0507/router/demo2.php/admin/user/index/id/1/name/admin/qq';
require __DIR__ . '/User.php';
//调用: admin\User::index()
//1.先获取类名
$className = $module . '\\' . ucfirst($controller);
p($className);
//2.获取参数
$params = array_splice($request,3);
// //按顺序传参处理如下:
// echo call_user_func_array([$className,$action],$params);
//按键名进行传参还需要如下处理
// p($params);
//把索引数组转为关联数组
//array_chunk():分组取出
$arr = array_chunk($params,2);
$result = [];
foreach($arr as $item){
[$key,$value] = $item;
$result[$key] = $value;
}
p($result);
//3.过滤空值
$result = array_filter($result);
p($result);
//4.调用
echo call_user_func_array([$className,$action],$result);
视图原理与实现
<?php
namespace phpcn;
class View
{
//约定:控制器方法的模板,默认控制器为目录名,方法为文件名
protected $controller;
protected $action;
protected $path;
//模板容器
protected $data = [];
//初始化创建模板的路径
public function __construct($controller,$action,$path='/view/')
{
$this->controller = $controller;
$this->action = $action;
$this->path = $path;
}
//模板赋值
public function assign($name,$value)
{
//$name 是外部变量在模板文件中的变量名
//$value 模板变量的值
$this->data[$name] = $value;
}
//模板渲染
public function render($path = '')
{
//展开模板变量数组
extract($this->data);
if(empty($path)){
//生成路径
$file = __DIR__ . $this->path . $this->controller . '/' . $this->action . '.php';
}else{
$file = $path;
}
//加载
// include $file or die('视图不存在');
file_exists($file) ? include $file : die('视图不存在');
}
}
//调用
$controller = 'User';
$action = 'hello';
$view = new View($controller,$action);
//模板赋值:变量
$view->assign('username','李先生');
//模板赋值:数组
$items = [
['name'=>'李龙','gongzi'=>5000],
['name'=>'刘强','gongzi'=>6000],
['name'=>'杨旭','gongzi'=>7000],
];
$view->assign('items',$items);
//渲染模板
$view->render();
数据库操作:增删查改
<?php
namespace phpcn;
use PDO;
class Db
{
protected $db;
protected $table;
protected $field;
protected $limit;
protected $opt = [];
public function __construct($dsn,$username,$password)
{
$this->db = new PDO($dsn, $username, $password);
}
public function table($table){
$this->table = $table;
return $this;
}
public function field($field){
$this->field = $field;
return $this;
}
public function limit($limit = 10){
$this->limit = $limit;
$this->opt['limit'] = " LIMIT $limit";
return $this;
}
public function page($page = 1)
{
//偏移量:offset = (page - 1) * limit
$this->opt['offset'] = ' OFFSET ' . ($page - 1) * $this->limit;
return $this;
}
//查询条件
public function where($where = '')
{
$this->opt['where'] = " WHERE $where";
return $this;
}
//查询
public function select()
{
//拼装sql
$sql = 'SELECT ' . $this->field . ' FROM ' . $this->table;
$sql .= $this->opt['where'] ?? null;
$sql .= $this->opt['limit'] ?? null;
$sql .= $this->opt['offset'] ?? null;
echo $sql . '<br>';
//预处理
$stmt = $this->db->prepare($sql);
$stmt->execute();
//清空条件
$this->opt['where'] = null;
return $stmt->fetchAll();
}
//插入
public function insert($data)
{
// [a=>1,b=2] => 'a=1,b=2'
$str ='';
foreach($data as $key=>$value){
$str .= $key . '="' . $value . '",';
}
$sql = 'INSERT ' . $this->table . ' SET ' . rtrim($str,',');
echo $sql .'<br>';
//预处理
$stmt = $this->db->prepare($sql);
$stmt->execute();
//清空条件
$this->opt['where'] = null;
return $stmt->rowCount();
}
//更新
public function update($data)
{
// [a=>1,b=2] => 'a=1,b=2'
$str ='';
foreach($data as $key=>$value){
$str .= $key . '="' . $value . '",';
}
$sql = 'UPDATE ' . $this->table . ' SET ' . rtrim($str,',');
echo $sql .'<br>';
//禁止无条件更新
$sql .= $this->opt['where'] ?? die('禁止无条件更新');
//预处理
$stmt = $this->db->prepare($sql);
$stmt->execute();
//清空条件
$this->opt['where'] = null;
return $stmt->rowCount();
}
//删除
public function delete()
{
$sql = 'DELETE FROM ' . $this->table;
echo $sql .'<br>';
//禁止无条件更新
$sql .= $this->opt['where'] ?? die('禁止无条件删除');
//预处理
$stmt = $this->db->prepare($sql);
$stmt->execute();
//清空条件
$this->opt['where'] = null;
return $stmt->rowCount();
}
}
//测试
//查询
$db = new Db('mysql:dbname=phpedu','root','phpedu');
// $result = $db->table('staff')->field('id,name,email')->select();
// $result = $db->table('staff')
// ->field('id,name,email')
// ->where('id > 1')
// ->limit(2)
// ->page(2)
// ->select();
// require __DIR__ . '/helper.php';
// p($result);
//新增
// $n = $db->table('staff')->insert(['name'=>'李先生','sex'=>0,'email'=>'li@qq.com']);
// 打印结果:INSERT staff SET name=李先生,email=li@qq.com,sex=0,
// echo $n > 0 ? '新增成功' . $n . '条<br>' : '新增失败'. $n . '条<br>';
//更新
// $n = $db->table('staff')->where('id = 18')->update(['name'=>'刘女士','sex'=>1]);
// echo $n > 0 ? '更新成功' . $n . '条<br>' : '更新失败'. $n . '条<br>';
//删除
$n = $db->table('staff')->where('id = 14')->delete();
echo $n > 0 ? '删除成功' . $n . '条<br>' : '删除失败'. $n . '条<br>';