
批改狀態(tài):合格
老師批語:
將模塊、控制器和方法從pathinfo中解析出來
user.php
<?php
// 用模塊當(dāng)命名空間
namespace admin;
class User
{
public static function index($id,$name)
{
printf('id=%d,name=%s',$id,$name);
}
}
<?php
require __DIR__.'/../helper.php';
// ! 主流路由解決方案:pathinfo
$url = 'http://phpedu.com/0507/router/demo2.php?c=user&a=hello';
p(pathinfo($url));
// 這個(gè)pathinfo不是我們要的
// 我們真正需要的是位于腳本名demo2.php與xxxx查詢字符串之間的路徑信息
$url2 = 'http://phpedu.com/0507/router/demo2.php/one/two/three?c=user&a=hello';
p(pathinfo($url2));
// p($_SERVER['PATH_INFO']);
// 以單一入口為例
// index.php?m=模塊,例如前臺home,后臺admin
// 單入口
// index.php/模塊/控制器/方法
// index.php/module/controller/action
// 多入口
// 前臺:index.php 作為入口 不需要模塊,controller/action
// 后臺:admin.php 作為入口 不需要模塊,controller/action
$url3 = 'http://phpedu.com/0507/router/demo2.php/admin/user/index';
p($_SERVER['PATH_INFO']);
p(explode('/',trim($_SERVER['PATH_INFO'],'/')));
$request = explode('/',trim($_SERVER['PATH_INFO'],'/'));
// 將模塊、控制器和方法解析出來
[$module,$controller,$action] = $request;
printf('模塊:%s<br>控制器:%s<br>方法:%s<br>',$module,$controller,$action);
// 從pathinfo中解析出參數(shù)
$url4 = 'http://phpedu.com/0507/router/demo2.php/admin/user/index/id/1/name/admin';
require 'User.php';
// admin\User::index(1,'張三');
// 類名
$className = $module.'\\'.ucfirst($controller);
p($className);
$params = array_splice($request,3);
printf('<pre>%s</pre>',print_r($params,true));
echo call_user_func_array([$className,$action],$params);
// p(array_chunk([1, 2, 3, 4, 5, 6, 7], 2));
$arr = array_chunk($params, 2);
p($arr);
$result = [];
foreach ($arr as $item) {
[$key, $value] = $item;
$result[$key] = $value;
}
p($result);
$result = array_filter($result);
p($result);
echo call_user_func_array([$className,$action],$result);
<body>
<h3>User控制器的hello()方法</h3>
<h3>Hello,<?=$username?></h3>
<h3>Hello,<?=$items?></h3>
<h3>Hello,<?=$lang?></h3>
<ul>
<?php foreach($items as ['name'=>$name,'price'=>$price]) : ?>
<li><?=$name?> : <?=$price?> 元</li>
<?php endforeach ?>
</ul>
<ul>
<?php foreach($lang as $value) : ?>
<li><?=$value?></li>
<?php endforeach ?>
</ul>
</body>
<?php
// 視圖基類
namespace phpcn;
class View
{
// 約定:控制器方法的模板,默認(rèn)一控制器為目錄名,以方法為文件名
protected $controller;
protected $action;
protected $path;
// 模板變量容器
protected $data = [];
// 初始化時(shí)創(chuàng)建模板的路徑
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='',$name=null,$value=null)
{
if($name && $value) $this->assign($name,$value);
// 展開模板變量數(shù)組
extract($this->data);
if(empty($path)){
// 按約定規(guī)則來生成模板文件的路徑并加載它
$file = __DIR__ . $this->path . $this->controller .'/' . $this->action . '.php';
}else{
$file = $path;
}
file_exists($file) ? include $file : die('視圖不存在');
}
}
// 測試
$controller = 'User';
$action = 'hello';
$view = new View($controller,$action);
// 模板賦值:變量
$view->assign('username','朱老師');
$items = [
['name'=>'手機(jī)','price'=>15000],
['name'=>'電腦','price'=>25000],
['name'=>'相機(jī)','price'=>35000],
];
$view->assign('items',$items);
// 渲染模板
// $view->render();
// 渲染,賦值二合一
$view->render($path = '', 'lang', ['php', 'java', 'python']);
<?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;
// 返回當(dāng)前對象,方便后面鏈?zhǔn)秸{(diào)用
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.'<hr>';
$stmt = $this->db->prepare($sql);
$stmt->execute();
// 清空查詢條件
$this->opt['where'] = null;
return $stmt->fetchAll();
}
$db = new Db('mysql:dbname=mydb','myshop','yzj123');
// $result = $db->table('staff')->field('id,name,email')->select();
$result = $db->table('staff')->field('id,name,email')
->where('id > 1')
->limit(2)
->page(3)
->select();
require 'helper.php';
p($result);
// 插入
public function insert($data)
{
// [a=>1,b=2] 'a=1, b=2'
$str = '';
foreach($data as $key=>$value){
$str .= $key.' = "' . $value . '", ';
}
// $str.=',';
// rtrim() 函數(shù)移除字符串右側(cè)的空白字符或其他預(yù)定義字符 rtrim(string,charlist)
$sql = 'INSERT '.$this->table.' SET '. rtrim($str,', ');
echo $sql.'<hr>';
$stmt = $this->db->prepare($sql);
$stmt->execute();
$this->opt['where'] = null;
return $stmt->rowCount();
}
$n = $db->table('staff')->insert(['name'=> 'zhu', 'email' => 'zhu@php.cn', 'sex' => 1, 'password' => md5(123456)]);
echo $n > 0 ? '新增成功<hr>' : '新增失敗或沒有數(shù)據(jù)被添加<hr>';
// 更新
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,', ');
$sql .= $this->opt['where'] ?? die('禁止無條件更新');
echo $sql.'<hr>';
$stmt = $this->db->prepare($sql);
$stmt->execute();
$this->opt['where'] = null;
return $stmt->rowCount();
}
$n = $db->table('staff')->where('id = 10')->update(['name' => 'Mrs_K']);
echo $n > 0 ? '更新成功<hr>' : '更新失敗或沒有數(shù)據(jù)被更新<hr>';
// 刪除
public function delete()
{
$sql = 'DELETE FROM '.$this->table;
$sql.= $this->opt['where'] ?? die('禁止無條件刪除');
echo $sql.'<hr>';
$stmt = $this->db->prepare($sql);
$stmt->execute();
$this->opt['where'] = null;
return $stmt->rowCount();
}
$n = $db->table('staff')->where('id = 10')->delete();
echo $n > 0 ? '刪除成功<hr>' : '刪除失敗或沒有數(shù)據(jù)被刪除<hr>';
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號