国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

MVC with PHP(二)

2024-05-04 22:53:41
字體:
來源:轉載
供稿:網友
mvc with php(一)中的bug的問題是存在,最大的問題是日志系統的問題,等完成這這個介紹后我后把全部更正的程序源碼打包
出來,這里就暫時不做更改了.
先來看看在application.class.php中是如何建立controller實例的:

php代碼:--------------------------------------------------------------------------------
/**
* 執行函數
*
* 此類唯一對外的一個接口
**/
public function run()
{
$this->parsepath();
$this->checksecurity($this->module, $this->action);
1. $controller = new $this->controllerclassname();
2. $controller->{$this->action}();
$this->writelog($this->module, $this->action);
}

--------------------------------------------------------------------------------

application這個類在實例后唯一可進行調用的一個函數,它根據用戶的url請求來分析得出所需要的controller類名,然后實例化這個類(上面標1的地方),再調用從url中獲取的動作名稱(上面標2的地方),

這個舉一個簡單的例子:
url: http://localhost/?module=news&action=showlist
application通過分析這個url重到controllerclassname=news, action=showlist,然后它將在包含處理這個controller類的文件名(在application->getcontrollerfile()中進行),然后實例化news這個
controller類(標1的地方), 隨后調用它的動作showlist(標2的地方).
來看看newscontroller.php中的內容:
=============================================================

php代碼:--------------------------------------------------------------------------------

<?php
/**
* filename: newscontroller.php
* introduce: 新聞控制類
*
* @author: 大師兄
* @email: [email protected]
* @version $id$
* @copyright 2004-10-26
**/
include_once ("./controller/comm/controller.class.php");
include_once ("./model/news/newsmodel.php");

class newscontroller extends controller
{
private $model;

/**
* 構造函數
*
**/
public function __construct()
{
parent::__construct();
$this->model = new newsmodel();
$this->setsmartytemplate_dir("./view/news");
}

/**
* 顯示新聞列表
*
**/
public function showlist()
{
1. $newslist = & $this->model->getlist();

2. $this->smarty->assign("newslist", $newslist);
3. unset($newslist);

4. $this->smarty->display("newslist.html");
}
}
?>

--------------------------------------------------------------------------------

==============================================================
首先,newscontroller類繼承自公共類controller,在類進行初始化時產生一個newsmodel類,這個類是一個model類,由這個類負責新聞模塊所有的對數據庫的交互. parent::__construct()調用父類的構造函數,完成對view的控制類smarty的初始化.$this->setsmartytemplate_dir("./view/news")將模板目錄定位在./view/news目錄.

然后看我們上面的例子,請求url為http://localhost/?module=news&actio...list,表示要調用
showlist這個動作,看newscontroller類的showlist()成員函數:
1. $newslist = & $this->model->getlist(): $this->model在newscontroller初始化時建立,這一句要使用$this->model從數據庫里提取出一個新聞列表,這個列表當然就是smarty在操作循環塊時需要的二維數組了,在newsmodel類中,它是采用adodb回傳的一個二維數組.
2. $this->smarty->assign("newslist", $newslist): 熟悉吧,smarty中循環塊的程序控制
3. unset($newslist):考慮到效率問題,對于這些臨時變量在使用完成后即時將它unset。
4. $this->smarty->display("newslist.html"):使用smarty來顯示view.

大家看明白了嗎?實際上controller類要做的事情就是這樣:1.調用model從數據庫取出記錄 2.操

作smarty顯示view。
再來看看newscontroller的父類controller類的源碼:
===========================================================

php代碼:--------------------------------------------------------------------------------

<?php
/**
* filename: controller.class.php
* introduce: base class of controller
*
* @author: 李曉軍
* @email: [email protected]
* @version $id$
* @copyright 2004-10-26
**/

include_once ("./comm/smarty/smarty.class.php");
include_once ("./comm/config.inc.php");

abstract class controller
{
private $smarty;

/**
* 系統構建函數
* 初始化smarty
**/
function __construct()
{
$this ->smarty = new smarty();

$this->smarty->template_dir = "./view/templates";
$this->smarty->compile_dir = "./view/templates_c";
$this->smarty->cache_dir = "./view/cache";
$this->smarty->cache_lifetime = 60 * 60 * 24;
$this->smarty->caching = false;
$this->smarty->left_delimiter = "<{";
$this->smarty->right_delimiter = "}>";
}

/**
* 設置smarty模板路徑
*
* @param string $template
**/
public function setsmartytemplate_dir($template)
{
$this->smarty->template_dir = $template;
}

/**
* 設置smarty是否進行緩存
*
* @param boolean $cache
**/
public function setsmartycache($cache = false)
{
$this->smarty->cache = $cache;
}

/**
* 設置smarty緩存時間
*
* @param string $cachelifetime
**/
public function setsmartycachetime($cachelifetime)
{
$this->smarty->cache_lifetime = $cachelifetime;
}

/**
* 動作被執行后一個短暫的提示
*
* @param string $module 重新定向到的模塊名稱
* @param string $action 重新定向的動作名稱
* @param string $params 參數名稱
* @param string $message 提示信息
**/
public function redirect($module, $action, $params="", $message="動作已經被成功執

行")
{
$time = wait_for_time;
$params = ("" == $params) ? "" : "&$params";
$url = "?module=" . $module . "&action=" . $action . $params;

//重新定smarty模板目錄至公用目錄
$this->setsmartytemplate_dir("./view/templates");
$this->smarty->assign("url", $url); //重定向的目錄
$this->smarty->assign("message", $message); //提示信息
$this->smarty->assign("time", $time);
$this->smarty->display("wait.html");
}

/**
* 調用本類不存在的方法時進行的處理
*
* @param string $name
* @param string $parameter
**/
public function __call($name, $parameter)
{
throw new actionnotallowexception("對不起,你所請求的動作 <b>$name</b> 沒有定義

...<br>");
}

/**
* 析構函數
*
**/
public function __destruct()
{
unset($this->smarty);
}
}
?>

--------------------------------------------------------------------------------

==============================================
controller是一個抽象類,也就是說它不可以直接使用new 來產生一個實例對象,在類的構造函數里產生一個smarty類,并對其進行基本的設置。其它的幾個函數是對smarty對象進行設置的成員函數, 這里來看看這兩個函數:

public function redirect($module, $action, $params="", $message="動作已經被成功執行"):
這是一個重新定向成員函數,它的作用是當我們對模塊進行一些操作后給出的提示頁面,然后經過設置好的時間自動重新定向到另一個位置,例如我們要對新聞進行一些刪除,刪除成功后我們要給用戶返回這樣一個頁面,告訴用戶操作已經成功,請待n秒后自動返回....這在論壇中是很常見的,這里我也引用了這樣的策略。

public function __call($name, $parameter):
當類調用類沒有聲明的函數時使用這個函數進行處理,這可是個好東東,有了它,可以使用對程序

的控制更加簡單了,大家可以試試這個方法....

好了,controller 部分就談到這里了。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 萨嘎县| 阿瓦提县| 仪陇县| 湘阴县| 乐东| 沧源| 得荣县| 阿坝| 清流县| 苍梧县| 城口县| 沁阳市| 古蔺县| 芦山县| 光山县| 龙岩市| 扎鲁特旗| 瑞安市| 车险| 会泽县| 昌平区| 嘉祥县| 疏附县| 日照市| 尉氏县| 祥云县| 平塘县| 洪湖市| 孟津县| 樟树市| 瑞金市| 潍坊市| 郴州市| 永新县| 阿拉善盟| 兴仁县| 高密市| 威海市| 平和县| 远安县| 奉贤区|