关于结合smarty进行MVC模式开发的讨论
最近公司要开发一套全新的系统,我打算用MVC模式开发,到网上搜罗了很多资料,但都不尽人意.
smarty很好的实现了MVC中的View,但是 Model 和 controller 好像还没有很好的区别.
phppatterns.com上面给出的MVC实现方法给了我很大的启发,但他并没有结合smarty
这是我个人结合smarty实现的,自认为比较清晰的MVC模式.本人不是想秀自己的水平,只是想和大家探讨一下.希望大家指出我的不足,或给点建议应如何改进
class ProductModel{
var $db;
function ProductModel(&$db)
{
$this->db=$db;
}
function listProducts(){
return $this->db->query($sql);
}
function getProductInfo()
{
return $this-db->query($sql);
}
}
class ProductView{
var $model;
var $template;
var $smarty;
function productView(&$model)
{
global $smarty;
$this->smarty=&$smarty;
$this->model = model;
}
function listProducts()
{
$smarty->assign('productlist',$this->model->listProducts());
$this->setTemplate('listProduct.ptl');
}
function getProductInfo()
{
$smarty->assign('productinfo',$this->model->getProductInfo());
$this->setTemplate('getProductInfo.ptl');
}
funciton setTemplate($template)
{
$this->template=$template;
}
function display()
{
$this->smarty->display($this->template);
}
}
class ProductController extends ProductView{
function ProductControler($model,$_GET)
{
ProductView::ProductView($model);
switch($_GET['action']){
case 'listProduct'
$this->listProduct();
break;
case 'getProductInfo'
$this->getProductInfo();
}
}
}
使用例子:
require_once('DB.php');
require_once('Smarty.class.php');
require_once('ProductModel.php');
require_once('ProductView.php');
require_once('ProudctController.php');
$model = & new ProductModel($db);
$controller = & new ProductController($model,$_GET);
$controller->display();