• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

在php中Controller向View传值

php 搞代码 3年前 (2022-01-23) 29次浏览 已收录 0个评论
文章目录[隐藏]

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

index.php

<code><?phprequire('View/testView.php');require('Model/testModel.class.php');require('Controller/testController.class.php');$testController = new testController();$testController->show();?></code>

testController.class.php

<code>class testController{    function show(){        $testModel = new testModel();        $data = $testModel->get();        return $data;    }}</code>

testModel.class.php

<code>require('database.php');get_connection();class testModel{    function get(){        $sql = "SELECT * FROM db_problem";        $res = mysql_query($sql);        return $res;    }}</code>

testView.php

<code>    <meta charset="UTF-8">    <title>BUG列表</title><body><table border="1px solid #bebebe" width="980px" cellpadding="1" cellspacing="0">    <tr>        <th width="10%">ID</th>        <th width="70%">问题</th>        <th width="20%">提交时间</th>    </tr>    <tr style="text-align: center">        <td></td>        <td></td>        <td></td>    </tr></table></code>

回复内容:

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

index.php

<code><?phprequire('View/testView.php');require('Model/testModel.class.php');require('Controller/testController.class.php');$testController = new testController();$testController->show();?></code>

testController.class.php

<code>class testController{    function show(){        $testModel = new testModel();        $data = $testModel->get();        return $data;    }}</code>

testModel.class.php

<code>require('database.php');get_connection();class testModel{    function get(){        $sql = "SELECT * FROM db_problem";        $res = mysql_query($sql);        return $res;    }}</code>

testView.php

<code>    <meta charset="UTF-8">    <title>BUG列表</title><body><table border="1px solid #bebebe" width="980px" cellpadding="1" cellspacing="0">    <tr>        <th width="10%">ID</th>        <th width="70%">问题</th>        <th width="20%">提交时间</th>    </tr>    <tr style="text-align: center">        <td></td>        <td></td>        <td></td>    </tr></table></code>

首先你得在控制器里指定模版比如 $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 $templat来&源gao@dai!ma.com搞$代^码%网搞gaodaima代码eData = []; //保存模板文件的数据映射表
public function index(){

<code> $this->assign($key,$value);</code>

}
public function assign($key,$value){

<code>   $this->assign($key,$value);</code>

}
public function display(){

<code>   /*加载view文件内容   /*正则搜索替换   /*输出</code>

}
}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:在php中Controller向View传值

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址