亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 98
粉絲 1
評論 0
訪問量 82794
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
路由原理與實(shí)現(xiàn)、視圖基類、模型與查詢構(gòu)造器基類學(xué)習(xí)
阿杰
原創(chuàng)
550人瀏覽過

一、路由原理與實(shí)現(xiàn)

  • 將模塊、控制器和方法從pathinfo中解析出來

  • user.php

  1. <?php
  2. // 用模塊當(dāng)命名空間
  3. namespace admin;
  4. class User
  5. {
  6. public static function index($id,$name)
  7. {
  8. printf('id=%d,name=%s',$id,$name);
  9. }
  10. }
  • 實(shí)例
  1. <?php
  2. require __DIR__.'/../helper.php';
  3. // ! 主流路由解決方案:pathinfo
  4. $url = 'http://phpedu.com/0507/router/demo2.php?c=user&a=hello';
  5. p(pathinfo($url));
  6. // 這個(gè)pathinfo不是我們要的
  7. // 我們真正需要的是位于腳本名demo2.php與xxxx查詢字符串之間的路徑信息
  8. $url2 = 'http://phpedu.com/0507/router/demo2.php/one/two/three?c=user&a=hello';
  9. p(pathinfo($url2));
  10. // p($_SERVER['PATH_INFO']);
  11. // 以單一入口為例
  12. // index.php?m=模塊,例如前臺home,后臺admin
  13. // 單入口
  14. // index.php/模塊/控制器/方法
  15. // index.php/module/controller/action
  16. // 多入口
  17. // 前臺:index.php 作為入口 不需要模塊,controller/action
  18. // 后臺:admin.php 作為入口 不需要模塊,controller/action
  19. $url3 = 'http://phpedu.com/0507/router/demo2.php/admin/user/index';
  20. p($_SERVER['PATH_INFO']);
  21. p(explode('/',trim($_SERVER['PATH_INFO'],'/')));
  22. $request = explode('/',trim($_SERVER['PATH_INFO'],'/'));
  23. // 將模塊、控制器和方法解析出來
  24. [$module,$controller,$action] = $request;
  25. printf('模塊:%s<br>控制器:%s<br>方法:%s<br>',$module,$controller,$action);
  26. // 從pathinfo中解析出參數(shù)
  27. $url4 = 'http://phpedu.com/0507/router/demo2.php/admin/user/index/id/1/name/admin';
  28. require 'User.php';
  29. // admin\User::index(1,'張三');
  30. // 類名
  31. $className = $module.'\\'.ucfirst($controller);
  32. p($className);
  33. $params = array_splice($request,3);
  34. printf('<pre>%s</pre>',print_r($params,true));
  35. echo call_user_func_array([$className,$action],$params);
  36. // p(array_chunk([1, 2, 3, 4, 5, 6, 7], 2));
  37. $arr = array_chunk($params, 2);
  38. p($arr);
  39. $result = [];
  40. foreach ($arr as $item) {
  41. [$key, $value] = $item;
  42. $result[$key] = $value;
  43. }
  44. p($result);
  45. $result = array_filter($result);
  46. p($result);
  47. echo call_user_func_array([$className,$action],$result);
  • 路由訪問

二、視圖基類

  • 數(shù)據(jù)展示頁面
  1. <body>
  2. <h3>User控制器的hello()方法</h3>
  3. <h3>Hello,<?=$username?></h3>
  4. <h3>Hello,<?=$items?></h3>
  5. <h3>Hello,<?=$lang?></h3>
  6. <ul>
  7. <?php foreach($items as ['name'=>$name,'price'=>$price]) : ?>
  8. <li><?=$name?> : <?=$price?></li>
  9. <?php endforeach ?>
  10. </ul>
  11. <ul>
  12. <?php foreach($lang as $value) : ?>
  13. <li><?=$value?></li>
  14. <?php endforeach ?>
  15. </ul>
  16. </body>
  • 視圖基類
  1. <?php
  2. // 視圖基類
  3. namespace phpcn;
  4. class View
  5. {
  6. // 約定:控制器方法的模板,默認(rèn)一控制器為目錄名,以方法為文件名
  7. protected $controller;
  8. protected $action;
  9. protected $path;
  10. // 模板變量容器
  11. protected $data = [];
  12. // 初始化時(shí)創(chuàng)建模板的路徑
  13. public function __construct($controller,$action,$path = '/view/')
  14. {
  15. $this->controller = $controller;
  16. $this->action = $action;
  17. $this->path = $path;
  18. }
  19. // 模板賦值
  20. public function assign($name,$value){
  21. // $name 是外部變量 在模板文件 中的變量名
  22. // $value 就是 模板變量的值
  23. $this->data[$name] = $value;
  24. }
  25. // 模板渲染
  26. // 將模板賦值與模板渲染二合一
  27. public function render($path='',$name=null,$value=null)
  28. {
  29. if($name && $value) $this->assign($name,$value);
  30. // 展開模板變量數(shù)組
  31. extract($this->data);
  32. if(empty($path)){
  33. // 按約定規(guī)則來生成模板文件的路徑并加載它
  34. $file = __DIR__ . $this->path . $this->controller .'/' . $this->action . '.php';
  35. }else{
  36. $file = $path;
  37. }
  38. file_exists($file) ? include $file : die('視圖不存在');
  39. }
  40. }
  41. // 測試
  42. $controller = 'User';
  43. $action = 'hello';
  44. $view = new View($controller,$action);
  45. // 模板賦值:變量
  46. $view->assign('username','朱老師');
  47. $items = [
  48. ['name'=>'手機(jī)','price'=>15000],
  49. ['name'=>'電腦','price'=>25000],
  50. ['name'=>'相機(jī)','price'=>35000],
  51. ];
  52. $view->assign('items',$items);
  53. // 渲染模板
  54. // $view->render();
  55. // 渲染,賦值二合一
  56. $view->render($path = '', 'lang', ['php', 'java', 'python']);

三、模型與查詢構(gòu)造器基類

  • Db模型
  1. <?php
  2. namespace phpcn;
  3. use PDO;
  4. class Db
  5. {
  6. protected $db;
  7. protected $table;
  8. protected $field;
  9. protected $limit;
  10. protected $opt = [];
  11. public function __construct($dsn,$username,$password)
  12. {
  13. $this->db = new PDO($dsn,$username,$password);
  14. }
  15. public function table($table)
  16. {
  17. $this->table = $table;
  18. // 返回當(dāng)前對象,方便后面鏈?zhǔn)秸{(diào)用
  19. return $this;
  20. }
  21. public function field($field)
  22. {
  23. $this->field = $field;
  24. return $this;
  25. }
  26. public function limit($limit=10)
  27. {
  28. $this->limit = $limit;
  29. $this->opt['limit'] = " LIMIT $limit";
  30. return $this;
  31. }
  32. // 分頁
  33. public function page($page=1){
  34. // 偏移量:offset = (page-1)*limit
  35. $this->opt['offset'] = ' OFFSET '.($page-1)*$this->limit;
  36. return $this;
  37. }
  38. // 查詢條件
  39. public function where($where = '')
  40. {
  41. $this->opt['where'] = " WHERE $where";
  42. return $this;
  43. }
  44. }

  • 查詢構(gòu)造器:查
  1. // 查詢
  2. public function select()
  3. {
  4. // 拼裝sql
  5. $sql = 'SELECT '.$this->field.' FROM '.$this->table;
  6. $sql .= $this->opt['where'] ?? null;
  7. $sql .= $this->opt['limit'] ?? null;
  8. $sql .= $this->opt['offset'] ?? null;
  9. echo $sql.'<hr>';
  10. $stmt = $this->db->prepare($sql);
  11. $stmt->execute();
  12. // 清空查詢條件
  13. $this->opt['where'] = null;
  14. return $stmt->fetchAll();
  15. }
  16. $db = new Db('mysql:dbname=mydb','myshop','yzj123');
  17. // $result = $db->table('staff')->field('id,name,email')->select();
  18. $result = $db->table('staff')->field('id,name,email')
  19. ->where('id > 1')
  20. ->limit(2)
  21. ->page(3)
  22. ->select();
  23. require 'helper.php';
  24. p($result);

  • 查詢構(gòu)造器:插入
  1. // 插入
  2. public function insert($data)
  3. {
  4. // [a=>1,b=2] 'a=1, b=2'
  5. $str = '';
  6. foreach($data as $key=>$value){
  7. $str .= $key.' = "' . $value . '", ';
  8. }
  9. // $str.=',';
  10. // rtrim() 函數(shù)移除字符串右側(cè)的空白字符或其他預(yù)定義字符 rtrim(string,charlist)
  11. $sql = 'INSERT '.$this->table.' SET '. rtrim($str,', ');
  12. echo $sql.'<hr>';
  13. $stmt = $this->db->prepare($sql);
  14. $stmt->execute();
  15. $this->opt['where'] = null;
  16. return $stmt->rowCount();
  17. }
  18. $n = $db->table('staff')->insert(['name'=> 'zhu', 'email' => 'zhu@php.cn', 'sex' => 1, 'password' => md5(123456)]);
  19. echo $n > 0 ? '新增成功<hr>' : '新增失敗或沒有數(shù)據(jù)被添加<hr>';

  • 查詢構(gòu)造器:更新
  1. // 更新
  2. public function update($data)
  3. {
  4. // [a=>1,b=>2] 'a=1,b=2'
  5. $str = '';
  6. foreach($data as $key=>$value){
  7. $str .= $key.' ="'.$value.'", ';
  8. }
  9. $sql = 'UPDATE '.$this->table.' SET '.rtrim($str,', ');
  10. $sql .= $this->opt['where'] ?? die('禁止無條件更新');
  11. echo $sql.'<hr>';
  12. $stmt = $this->db->prepare($sql);
  13. $stmt->execute();
  14. $this->opt['where'] = null;
  15. return $stmt->rowCount();
  16. }
  17. $n = $db->table('staff')->where('id = 10')->update(['name' => 'Mrs_K']);
  18. echo $n > 0 ? '更新成功<hr>' : '更新失敗或沒有數(shù)據(jù)被更新<hr>';

  • 查詢構(gòu)造器:刪除
  1. // 刪除
  2. public function delete()
  3. {
  4. $sql = 'DELETE FROM '.$this->table;
  5. $sql.= $this->opt['where'] ?? die('禁止無條件刪除');
  6. echo $sql.'<hr>';
  7. $stmt = $this->db->prepare($sql);
  8. $stmt->execute();
  9. $this->opt['where'] = null;
  10. return $stmt->rowCount();
  11. }
  12. $n = $db->table('staff')->where('id = 10')->delete();
  13. echo $n > 0 ? '刪除成功<hr>' : '刪除失敗或沒有數(shù)據(jù)被刪除<hr>';

批改老師:PHPzPHPz

批改狀態(tài):合格

老師批語:
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報(bào)處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)