AMP開發(fā)技術(shù)文檔
AMP系統(tǒng)目錄結(jié)構(gòu) ------------------------------------------------------------------
Amysql) 系統(tǒng)目錄
| Amysql.php
| Config.php
Controller) 控制器文件目錄
Model) 數(shù)據(jù)模型文件目錄
View) 視圖模板文件目錄
| AmysqlNotice.php
Class) 對(duì)象類文件目錄
index.php
AMP運(yùn)行流程 ---------------------------------------------------------------------------
為了更好的掌握應(yīng)用AMP,我們先來簡(jiǎn)單了解下AMP運(yùn)行流程。
01) index.php ? 載入配置文件、系統(tǒng)文件進(jìn)入系統(tǒng)層(A層)。實(shí)例化主進(jìn)程Amysql、實(shí)例化進(jìn)程AmysqlProcess ? 分析用戶請(qǐng)求控制器與方法并執(zhí)行。
02) 執(zhí)行用戶控制器方法進(jìn)行邏輯處理層(C層)同時(shí)初始化視圖模板層(V層),此時(shí)用戶控制器(C層)繼承于系統(tǒng)AmysqlController,擁有系統(tǒng)載入數(shù)據(jù)模型、引入新類、模板數(shù)據(jù)處理等方法。
03) (C層)中可載入用戶自定義數(shù)據(jù)模型(M層),此時(shí)用戶數(shù)據(jù)模型(M層)同樣繼承于系統(tǒng)AmysqlModel,擁有Mysql讀寫等操作方法。(無數(shù)據(jù)請(qǐng)求可略此過程)
04) (C層)賦值于模板(V層)。
05) (V層)顯示完成。
A ? C ? M ? V 很簡(jiǎn)單,這就是MVC,似乎ACMV更好理解。
簡(jiǎn)單應(yīng)用AMP -----------------------------------------------------------------------
01) 首先需要簡(jiǎn)單配置下系統(tǒng)文件,進(jìn)入Amysql系統(tǒng)目錄。打開配置文件Config.php。
文件當(dāng)中有注釋各參數(shù)的使用用途,其中可配置Url模式、過濾、調(diào)試、編碼、名稱與其數(shù)據(jù)庫默認(rèn)連接等基本信息(無Mysql應(yīng)用話可無需配置Mysql信息)
02) 簡(jiǎn)單配置完成后下一步創(chuàng)建控制器文件。進(jìn)入Controller文件目錄創(chuàng)建控制器文件index.php寫入以下代碼:(提示:系統(tǒng)默認(rèn)運(yùn)行的控制器為index、默認(rèn)的執(zhí)行方法為IndexAction,同時(shí)要注意控制器的class名稱需與文件名稱保持一致。)
<?php class index extends AmysqlController { function IndexAction() { echo 'Hello World'; }
function HelloAMP() { echo 'Hello AMP'; } } ?>
03) 打開http://localhost/AMP/ 就可看到瀏覽器輸出Hello World信息。 打開http://localhost/AMP/index.php/index/HelloAMP 或http://localhost/AMP/index.php?c=index&a=HelloAMP 就可看到Hello AMP。至此您已完成了偉大Hello World例子。
AMP輕松上手,控制器與模型方法 -----------------------------------------------------------------------
控制器常用的一些方法。
<?php class index extends AmysqlController { function IndexAction() { $this -> name = $name; // 簡(jiǎn)單賦值 $this -> _DBSet($Config); // 如有設(shè)置此項(xiàng)同時(shí)可進(jìn)行多個(gè)mysql連接。 $Config : 配置 $this -> _array($array); // 數(shù)組多模板變量賦值 $array : 數(shù)據(jù)
$this -> _echo($data); // 數(shù)據(jù)調(diào)試 $this -> _view($file); // 模板顯示
// 加載自定義類文件 $file 文件名, $ClassName 類名(可選,默認(rèn)為文件名) $this -> _class($file, $ClassName);
// 加載模型文件 $file 文件名, $ClassName 類名(可選,默認(rèn)為文件名) $this -> _model($file, $ClassName);
// 獲得相關(guān)文件數(shù)據(jù)(模板目錄) $file 文件名 $this -> _file($file); } } ?>
模型常用的一些方法。
<?php class datas extends AmysqlModel { function databases() { $this -> _query($sql); // 執(zhí)行Sql
// 更新數(shù)據(jù) $table表名,$data數(shù)據(jù)array('field' => 'value'), $where條件Sql $this -> _update($table, $data, $where = '');
// 插入數(shù)據(jù) $table表名,$data數(shù)據(jù)array('field' => 'value') $this -> _insert($table, $data);
$this -> _row($sql); // 取得一行數(shù)據(jù) $this -> _all($sql); // 取得所有數(shù)據(jù) $this -> _sum($sql); // 取得數(shù)據(jù)總數(shù) } } ?>
AMP應(yīng)用實(shí)例 --------------------------------------------------------------------
下面通過一實(shí)例,介紹Mysql數(shù)據(jù)調(diào)用、引用新class、模板數(shù)據(jù)賦值、調(diào)試等方法。
01) 數(shù)據(jù)模型文件創(chuàng)建,新class文件創(chuàng)建,準(zhǔn)備數(shù)據(jù)于控制器調(diào)用。
創(chuàng)建模型 Model/index.php 文件。
<?php class datas extends AmysqlModel { function databases() { $sql = "SHOW DATABASES"; Return $this -> _all($sql); // _all方法取得所有數(shù)據(jù)返回 } } ?>
創(chuàng)建模型 Model/tables.php 文件。
<?php class tables extends AmysqlModel { function GetTables() { $sql = "SHOW TABLES"; Return $this -> _row($sql); // _row方法取得一行數(shù)據(jù)返回 } } ?>
創(chuàng)建新對(duì)象 Class/NewClass.php 文件。
<?php class NewClass { public $name = 'NewClass'; function GetName() { Return $this -> name; } } ?>
02) 控制器創(chuàng)建
創(chuàng)建 Controller/index.php 文件。
<?php
class index extends AmysqlController { function IndexAction() { // 1) 模板如何賦值 $this -> content = 'Hello World'; // 單一賦值模板$content變量方式 $this -> MyName = 'Amysql (AMP)'; $this -> ArrayData = array('Website' => 'http://', 'ProductType' => 'PHP MVC'); $this -> _array(array('NameA' => 'A', 'NameB' => 'B')); // _array方法數(shù)組賦值模板變量$NameA、$NameB
// 2) 如何連接Mysql查詢數(shù)據(jù) $IndexModel = $this -> _model('index', 'datas'); // 載入步驟1)創(chuàng)建的index模型文件datas模型對(duì)象 $this -> databases = $IndexModel -> databases(); // 執(zhí)行模型的databases方法取得數(shù)據(jù)直接賦值模板
// 2.1) 可同時(shí)創(chuàng)建新Mysql連接 $this -> _DBSet(array('DBname' => 'information_schema', 'ConnectTag' => 'NewContent')); // 鏈接新數(shù)據(jù)庫 設(shè)置新的Mysql連接信息
$tables = $this -> _model('tables'); // 載入步驟1)創(chuàng)建的tables模型文件的tables模型對(duì)象 $this -> tables = $tables -> GetTables(); // 執(zhí)行模型的GetTables方法取得數(shù)據(jù)直接賦值模板
// 3) 如何引入新Class對(duì)象 $EchoName = $this -> _class('NewClass') -> GetName(); // 載入步驟1)創(chuàng)建NewClass類文件的NewClass對(duì)象并執(zhí)行GetName方法
// 4) 信息調(diào)試 $this -> _echo($EchoName); // 信息輸出在網(wǎng)頁源代碼最底部
$this -> _view('index'); // 調(diào)用模板輸出 } } ?>
03) 模板文件創(chuàng)建
創(chuàng)建 View/index.php 文件。
<? // 添加以下模板代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <html> <head> <title><?php echo $content;?></title> </HEAD>
<BODY> <p><?php echo $MyName;?></p> <p><?php echo $content;?></p> <p>NameA : <?php echo $NameA;?></p> <p>NameB : <?php echo $NameB;?></p>
<pre> <?php print_r($ArrayData); print_r($databases); print_r($tables); ?> </pre> </BODY> </HTML>
04) 到這里已完成了,打開http://localhost/AMP/ 就可看到瀏覽器輸出模板各變量數(shù)據(jù)信息。
最后您還需簡(jiǎn)單了解一下這些參數(shù) -----------------------------------------------------------------------
系統(tǒng)一些目錄參數(shù)與系統(tǒng)各模塊對(duì)象關(guān)系
<?php class index extends AmysqlController { function IndexAction() { echo _Host , '<br />'; // 主機(jī)網(wǎng)址 echo _Http , '<br />'; // 網(wǎng)站根目錄網(wǎng)址,常用。 echo _ROOT , '<br />'; // 網(wǎng)站根目錄 echo _Amysql , '<br />'; // 系統(tǒng)目錄 echo _Controller , '<br />'; // 控制器目錄 echo _Model , '<br />'; // 模型目錄 echo _Class , '<br />'; // 對(duì)象類目錄 echo _View , '<br />'; // 視圖模板目錄
/********************* * 深入底層 > 系統(tǒng)各模塊關(guān)聯(lián)調(diào)用 * 靈活掌握應(yīng)用,請(qǐng)查看$Amysql各對(duì)象關(guān)系。 */ global $Amysql; echo '<pre>'; print_r($Amysql); echo '</pre>'; } } ?>
End -----------------------------------------------------------------------
到這里AMP的應(yīng)用介紹文檔就結(jié)束了,感謝選擇AMP框架,
如有使用有任何疑問請(qǐng)點(diǎn)這里反饋。
|