這是一個 5 部分系列文章的第 3 部分,本部分通過增加一些可以刪除、添加、更新汽車記錄的功能繼續構建這個 Web Automobile Sales Platform。本部分還將介紹如何使用驗證分離用戶功能和管理功能。
簡介
|
本系列的第 2 部分帶您深入 Agavi 的世界,介紹如何處理用戶通過 Web 表單提交的用戶輸入,如何通過來自 MySQL 和 Doctrine 的幫助在您的應用程序中支持資料庫訪問。通過將 Model 添加到這個混合程序並使用這些 Model 讀取來自應用程序資料庫的車輛清單,該部分還拓展了您對 Agavi 的 MVC 實現的知識。
但是,了解如何從資料庫讀取記錄只解決了問題的一半。另一半涉及寫入新記錄或修改現有記錄,本文將解決這個問題。在接下來的幾節中,我將幫助您打造一個更智能的 Web Automobile Sales Platform (WASP) 示例應用程序,以便用戶能夠通過一個 Web 界面創建、編輯和刪除記錄。我們還將探討 Agavi 的安全框架的基礎理論,展示如何將某些功能限制到只允許經過驗證的用戶使用。現在,就讓我們開始吧!
添加資料庫記錄
首先,圖 1 將幫助您迅速回憶起這個 WASP 資料庫的結構:
本系列的第 2 部分結束時創建了一個 DisplayAction,它從資料庫讀取並顯示單獨的車輛清單。這些清單本身是在 MySQL 命令提示中使用原始 SQL 命令手動創建的。但是,這個 WASP 應用程序的目標是支持銷售商自己向資料庫添加清單,管理員可以在資料庫中審查並確認這些清單。這個業務目標自然會導致以下功能要求:
要實現上述要求,首先構建一個允許銷售商通過一個 Web 表單添加新清單的 CreateAction。啟動您的 Agavi 構建腳本並初始化 Listing 模塊中的 Action 和 3 個視圖,如下所示:
shell> agavi action-wizard Module name: Listing Action name: Create Space-separated list of views to create for Create [Success]: Input Error Success |
您還需要將這個 CreateAction 的一個新路由添加到應用程序的路由表(參見 清單 1):
<?xml version="1.0" encoding="UTF-8"?> <ae:configurations xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0" xmlns="http://agavi.org/agavi/config/parts/routing/1.0"> <ae:configuration> <routes> ... <!-- action for listing pages "/listing" --> <route name="listing" pattern="^/listing" module="Listing"> <route name=".create" pattern="^/create___FCKpd___1quot; action="Create" /> <route name=".display" pattern="^/display/(id:\d+)___FCKpd___1quot; action="Display" /> </route> </routes> </ae:configuration> </ae:configurations> |
這個 CreateAction 的默認行為是顯示一個 Web 表單,該表單的欄位與 圖 1 中顯示的數據欄位一致。為此,通過定義 getDefaultViewName() 方法(參見 清單 2)在 CreateAction 中指定 CreateInputView 默認顯示所有 GET 請求。
<?php class Listing_CreateAction extends WASPListingBaseAction { public function getDefaultViewName() { return 'Input'; } } ?> |
使用必要的 Web 表單更新對應的 CreateInput 模板文件(參見 清單 3)。本文附帶的代碼壓縮文檔(參見 下載)包含這個表單的 CSS 規則和 JavaScript 代碼。
<script src="/js/form.js"></script> <h3>Add Listing</h3> <form action="<?php echo $ro->gen(null); ?>" method="post"> <fieldset> <legend>Owner Information</legend> <label for="OwnerName" class="required">Name:</label> <input id="OwnerName" type="text" name="OwnerName" /> <p/> <label for="OwnerTel">Telephone number:</label> <input id="OwnerTel" type="text" name="OwnerTel" /> <p/> <label for="OwnerEmail" class="required">Email address:</label> <input id="OwnerEmail" type="text" name="OwnerEmail" /> <p/> <label for="OwnerCity" class="required">City:</label> <input id="OwnerCity" type="text" name="OwnerCity" /> <p/> <label for="OwnerCountryID" class="required">Country:</label> <select id="OwnerCountryID" name="OwnerCountryID"> <?php foreach ($t['countries'] as $c): ?> <?php echo "<option value=\"$c[CountryID]\">" . $c['CountryName'] . "</option>"; ?> <?php endforeach; ?> </select> <p/> </fieldset> <fieldset> <legend>Vehicle Information</legend> <label for="VehicleManufacturerID" class="required">Manufacturer:</label> <select id="VehicleManufacturerID" name="VehicleManufacturerID"> <?php foreach ($t['manufacturers'] as $m): ?> <?php echo "<option value=\"$m[ManufacturerID]\">" . $m['ManufacturerName'] . "</option>"; ?> <?php endforeach; ?> </select> <p/> <label for="VehicleModel" class="required">Model:</label> <input id="VehicleModel" type="text" name="VehicleModel" /> <p/> <label for="VehicleYear" class="required">Year of manufacture:</label> <input id="VehicleYear" type="text" name="VehicleYear" size="4" style="width:80px" /> <p/> <label for="VehicleColor" class="required">Color:</label> <input id="VehicleColor" type="text" name="VehicleColor" /> <p/> <label for="VehicleMileage" class="required">Mileage:</label> <input id="VehicleMileage" type="text" name="VehicleMileage" size="6" style="width:100px" /> <p/> <label for="VehicleAccessoryBit" style="height:130px">Accessories:</label> <input id="VehicleAccessoryBit_1" type="checkbox" name="VehicleAccessoryBit[]" value="1" style="width:2px" />Power steering <br/> <input id="VehicleAccessoryBit_2" type="checkbox" name="VehicleAccessoryBit[]" value="2" style="width:2px" />Power windows <br/> <input id="VehicleAccessoryBit_4" type="checkbox" name="VehicleAccessoryBit[]" value="4" style="width:2px" />Audio system <br/> <input id="VehicleAccessoryBit_8" type="checkbox" name="VehicleAccessoryBit[]" value="8" style="width:2px" />Video system <br/> <input id="VehicleAccessoryBit_16" type="checkbox" name="VehicleAccessoryBit[]" value="16" style="width:2px" />Keyless entry system <br/> <input id="VehicleAccessoryBit_32" type="checkbox" name="VehicleAccessoryBit[]" value="32" style="width:2px" />GPS <br/> <input id="VehicleAccessoryBit_64" type="checkbox" name="VehicleAccessoryBit[]" value="64" style="width:2px" />Alloy wheels <p/> <label for="data[VehicleIsFirstOwned]">Ownership:</label> <input id="data[VehicleIsFirstOwned]" type="checkbox" name="data[VehicleIsFirstOwned]" value="1" style="width:2px" />First owner <p/> <label for="VehicleIsCertified">Certification:</label> <input id="VehicleIsCertified" type="checkbox" name="VehicleIsCertified" value="1" style="width:2px" onClick="javascript:handleInputDisplayOnCheck('VehicleIsCertified', 'divVehicleCertificationDate')"/>Fully certified <p/> <div id="divVehicleCertificationDate" style="display:none"> <label for="VehicleCertificationDate" class="required"> Certificate issued in:</label> <select id="VehicleCertificationDate_mm" name="VehicleCertificationDate_mm"> <?php for ($x=1; $x<=12; $x++): ?> <?php echo "<option value=\"$x\">" . date("F", mktime(null, null, null, $x, 1)) . "</option>"; ?> <?php endfor; ?> </select> <select id="VehicleCertificationDate_yyyy" name="VehicleCertificationDate_yyyy"> <?php for ($x=1990; $x<=date('Y'); $x++): ?> <?php echo "<option value=\"$x\">$x</option>"; ?> <?php endfor; ?> </select> <p/> </div> <label for="VehicleSalePriceMin" class="required">Sale price (min): </label> <input id="VehicleSalePriceMin" type="text" name="VehicleSalePriceMin" size="6" style="width:100px" /> <p/> <label for="VehicleSalePriceMax" class="required">Sale price (max): </label> <input id="VehicleSalePriceMax" type="text" name="VehicleSalePriceMax" size="6" style="width:100px" /> <p/> <label for="VehicleSalePriceIsNegotiable"> </label> <input id="VehicleSalePriceIsNegotiable" type="checkbox" name="VehicleSalePriceIsNegotiable" value="1" style="width:2px" />Negotiable <p/> <label for="Note">Description:</label> <textarea id="Note" name="Note" style="width:300px; height:200px" ></textarea> <p/> </fieldset> <input type="submit" name="submit" class="submit" value="Submit Listing" /> </form> <script> handleInputDisplayOnCheck('VehicleIsCertified', 'divVehicleCertificationDate'); </script> |
[火星人 ] 使用 Agavi 進行 MVC 編程簡介,第 3 部分: 使用 Agavi 添加驗證已經有833次圍觀