php controller view,在php中Controller向View传值

想用MVC的方式写一个小的CMS,建立了Controller、Model、View,但是不知道Controller向view传值应该怎么写?

index.php

show();

?>

testController.class.php

class testController{

function show(){

$testModel = new testModel();

$data = $testModel->get();

return $data;

}

}

testModel.class.php

require('database.php');

get_connection();

class testModel{

function get(){

$sql = "SELECT * FROM db_problem";

$res = mysql_query($sql);

return $res;

}

}

testView.php

BUG列表

ID

问题

提交时间

回复内容:

想用MVC的方式写一个小的CMS,建立了Controller、Model、View,但是不知道Controller向view传值应该怎么写?

index.php

show();

?>

testController.class.php

class testController{

function show(){

$testModel = new testModel();

$data = $testModel->get();

return $data;

}

}

testModel.class.php

require('database.php');

get_connection();

class testModel{

function get(){

$sql = "SELECT * FROM db_problem";

$res = mysql_query($sql);

return $res;

}

}

testView.php

BUG列表

ID

问题

提交时间

首先你得在控制器里指定模版比如 $this->display('test'); 然后在display方法中把模版include进来就行了

如果想复杂一点,给模版增加语法糖,就在display中判断该模版是否有编译过后的文件,没有的话则执行编译(实质上就是正则替换,比如{$test}替换为$this->test),然后include编译后的文件

这样就可以直接使用控制器的变量了

之前写过一个简单的mvc框架,你可以参考一下,核心内容在钱158行https://github.com/eyblog/mvc...

在controller里面把模板中的变量和值存在数据里面,file_get_content读取view视图文件内容,模板变量标识可以按照你喜欢的来,比如{$user}或{{user}},然后正则表达式匹配替换,最后输入echo

class Controller {

public $templateData = []; //保存模板文件的数据映射表

public function index(){

$this->assign($key,$value);

}

public function assign($key,$value){

$this->assign($key,$value);

}

public function display(){

/*加载view文件内容

/*正则搜索替换

/*输出

}

}

相关标签:php

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值