您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

在php中Controller向View传值

2024/3/29 8:02:43发布5次查看
想用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文件内容 /*正则搜索替换 /*输出
}
}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录