关于结合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();
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();
...全文
相关推荐
php+smarty+mvc框架登录已完成
通过这个项目,开发者可以进一步学习如何将Smarty模板引擎和MVC设计模式结合,以及如何在Windows环境下搭建和调试PHP应用。
PHP原生模板引擎 最简单的模板引擎
在相关内容中,提到了其他可能感兴趣的文章,涵盖了ThinkPHP框架中使用Smarty、PHP模板引擎的比较分析、Smarty的缓存使用、PHP模板引擎的简单实现、MVC模式下的模板引擎开发经验,以及Yii
php开发宝典
ThinkPHP是一个开源的轻量级PHP开发框架,它遵循MVC(模型-视图-控制器)设计模式。ThinkPHP以简洁实用为核心,它的设计目标是帮助开发者快速完成企业级应用开发。
PHP开发框架关键技术分析.pdf
PHP开发框架关键技术分析本文对PHP开发框架关键技术进行了分析和讨论,涵盖了PHP开发框架中的MVC设计模式、模板解析引擎和数据访问层等技术。
PHP100视频教程61:PHP.Mysql新闻系统的开发一.rar
MVC模式应用:在实际开发中,Model-View-Controller(MVC)模式被广泛采用,它能提高代码的可维护性和可扩展性。我们将了解MVC的基本概念,以及如何在PHP中实现这一模式。


基础编程
