ThinkPHP是目前國內應用非常廣泛的一款MVC開發框架。本文就以實例形式解析ThinkPHP的MVC開發機制。相信會給大家一定的啟發作用。具體分析如下:
一、概述:
MVC框架解析如下:
M Model層 模型:就是數據庫操作類(通過數據庫操作類去操作各個表)
V View層 視圖:指模版。
C Control層 控制器:就是通過控制器來實現模版,模型之間的控制關系。
二、實例分析:
1.ACTION 控制器:
位置 D:\www\aoli\Home\Lib\Action\IndexAction.class.php
代碼如下:
public function test() //訪問路徑:http://localhost/aoli/index.php/Index/test
{
$Test=D('Test');//實例化Model
//$list=$Test->select();
$list=$Test->query("select * from test" );
$this->assign('list',$list);
$this->assign('title','彭艷傑');
$this->display();
}
public function index() //index對應aoli\Tpl\default\Index下的index.html
{
$this->display();
}
2.MODEL 模型:
位置 D:\www\aoli\Home\Lib\Model\IndexModel.class.php
代碼如下:
<?php
class TestModel extends Model{ //對應數據庫中的表test
//可在此處添加操作數據庫表的類
}
?>
3.VIEW 視圖:
位置 D:\www\aoli\Home\Tpl\default\Index\test.html
代碼如下:
<p style=" font-weight:bold; line-height:22px;">{$title}</p>
<div style=" color:#00F;">
<volist name="list" id="vo">
<p>{$vo.title} - {$vo.con}</p>
</volist>
</div>
感興趣的朋友可以調試運行一下本文所述實例以加深理解。希望本文所述對於大家學習ThinkPHP有所幫助。