1 / 22

MVC Anjeng

MVC Anjeng. 傳統程式架構. 一個功能一個頁面 以產品管理為例 Index.php ( 產品列表頁 ) Add_product.php ( 新增產品表單 ) Do_add_product.php ( 處理新增產品 ) Edit_product.php ( 修改產品表單 ) Do_edit_product.php ( 處理修改產品 ) Do_delete_product.php ( 處理刪除產品 ) 顯而易見地 , 傳統程式架構是一個功能一支程式. 傳統程式 架構 – 產品列表頁. <? php // 進行資料庫連結

flavio
Télécharger la présentation

MVC Anjeng

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MVCAnjeng

  2. 傳統程式架構 • 一個功能一個頁面 • 以產品管理為例 • Index.php (產品列表頁) • Add_product.php(新增產品表單) • Do_add_product.php(處理新增產品) • Edit_product.php(修改產品表單) • Do_edit_product.php(處理修改產品) • Do_delete_product.php(處理刪除產品) • 顯而易見地, 傳統程式架構是一個功能一支程式

  3. 傳統程式架構 –產品列表頁 <?php // 進行資料庫連結 // .... (略) .... ?> // 輸出 HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head> .... (略) .... <table><tr><th>產品編號</th><th>產品品名</th><th>成本</th><th>售價</th><th>庫存數量</th></tr> <?php // 從資料庫取得產品資料,並輸出成表格 $select_sql = "SELECT `id`, `name`, `price`, `cost` FROM `idic_product` "; $result = mysql_query($select_sql); while ($row = mysql_fetch_array($result)) { echo "<tr><td>$row[‘id’]</td><td>$row[‘name’]</td><td>$row[‘cost’]</td><td>$row[‘price’]</td><td>$row[‘qty’]</td></tr>"; } ?> </table> .... (略) .... </body></html>

  4. 傳統程式架構 – 新增產品表單及處理新增產品 • 新增產品表單純粹為表單頁,因此略過 <?php // 進行資料庫連結 // .... (略) .... // 取得表單變數 $name = $_POST['name']; $price = $_POST['price']; $cost = $_POST['cost']; $qty = $_POST['qty']; // 組成新增SQL語法 $insert_sql = "INSERT INTO `idic_product` (`name`, `price`, `cost`, `qty` VALUES ( '$name', '$price', '$cost', '$qty' )"; // 將資料新增進資料庫 mysql_query($insert_sql); // 導回首頁 header('Location: ./'); ?>

  5. 傳統程式架構優缺點 • 優點在於開發小型專案可能速度較快 • 缺點 • 多人開發情況下,程式開發與視覺設計無法並行 • 版型頁面風格改變則要修改所有與畫面有關程式 • 重複的程式碼太多(例如讀取資料庫部份) • 一旦發現錯誤,必須把所有相關程式找出來修改 • 程式邏輯與HTML畫面混在一起 • <?php …?> <html>… <?php …?> …</html> • 一個功能一隻程式 • index.php, add_product.php, do_add_product.php, do_delete_product.php

  6. Model-View-Controller 介紹 • 軟體開發的一種設計模式 • 將程式分成 • 模組(Model) • 負責與資料庫的操作, 如新增, 刪除, 修改和查詢 • 視窗(View) • 負責從模組取得之資料, 組成畫面 • 控制項(Controller) • 負責處理使用者要求(function),並根據要求與View和Model互動

  7. MVC介紹

  8. MVC好處與壞處 • 優點 • 程式邏輯與畫面邏輯清楚地切割 • 程式設計師與畫面設計師可以平行開發 • 維護及擴充容易 • 缺點 • 複雜度增加 • 程式設計師與畫面設計師需具備MVC開發關念 • 規劃時間拉長, 不適合小型專案

  9. MVC介紹 -以產品管理模組為例 • 需求描述 • 可透過此模組管理產品 • 模組介面與功能分析 • 需要產品列表, 新增產品及修改產品等介面 • 功能則有新增, 修改, 刪除, 列表等功能 • 修改產品畫面 • 新增產品畫面 • 產品列表畫面

  10. MVC介紹 -以產品管理模組為例

  11. MVC介紹 -以產品管理模組為例

  12. MVC介紹 – 連結產品列表為例

  13. MVC介紹 – 連結產品列表為例

  14. MVC介紹 – 連結產品列表為例 • index.php • Controller <?php // 進行資料庫連結 // .... (略) .... require_once“view.inc.php"; $func = $_GET['func']; switch($func) { // ...(略)... case 'show_product_list_page': echo showProductListPage(); break; // ...(略)... } ?>

  15. MVC介紹 – 連結產品列表為例 • view.php • View <?php // 載入 Smarty require_once "model.inc.php"; // ...(略)... showProductListPage() { // Smarty初始化 // 取得產品列表 $product_list = getProductList(); //設定樣版變數 $tpl->assign('product_list', $product_list); //回傳套用後結果給Controller return $tpl->fetch('show_product_list_page.tpl'); } // ...(略)... ?>

  16. MVC介紹 – 連結產品列表為例 • model.php • Model <?php // 載入 ezSQL // ...(略)... getProductList() { // ezSQL初始化 // 進行資料庫查詢並將結果回傳給View return $db->get_results("SELECT ... "); } // ...(略)... ?>

  17. MVC介紹 – 刪除產品資料

  18. MVC介紹 – 刪除產品資料

  19. MVC介紹 – 刪除產品資料 • index.php • Controller <?php // 進行資料庫連結 // .... (略) .... require_once“view.inc.php"; $func = $_GET['func']; switch($func) { // ...(略)... case ‘action_delete_producdt': $product_id = $_POST[‘product_id’]; deleteProduct($product_id); header('Location: ./'); break; // ...(略)... } ?>

  20. MVC介紹 – 刪除產品資料 • view.php • View

  21. MVC介紹 – 刪除產品資料 • model.php • Model <?php // 載入 ezSQL // ...(略)... deleteProduct($product_id) { // ezSQL初始化 // 進行刪除資料 $db->get_results(“DELETE FROM product WHERE `product_id` = ‘$product_id’ "); } // ...(略)... ?>

  22. MVC的使用時機與函數命名原則 • Model- 負責與資料庫的操作 • 依照對資料庫操作之動作命名 • 如:新增產品, insertProduct, 刪除產品則, deleteProduct, 修改, updateProduct, 查詢, getProduct • View - 負責產生使用者畫面 • 依照顯示畫面內容命名 • 如:顯示產品清單, showProductListPage, 顯示新增畫面, showInsertProductPage • Controller - 負責處理於使用者的溝通

More Related