在php中Controller向View传值
想用MVC的方式写一个小的CMS,建立了Controller、Model、View,但是不知道Controller向view传值应该怎么写?
index.php
<code><?php require('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> <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><?php require('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> <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 $templateData = []; //保存模板文件的数据映射表
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>
}
}

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

登录 CakePHP 是一项非常简单的任务。您只需使用一项功能即可。您可以记录任何后台进程(如 cronjob)的错误、异常、用户活动、用户采取的操作。在 CakePHP 中记录数据很容易。提供了 log() 函数

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写
