一步一步重写 CodeIgniter 框架 (7) —— Controller执行时将 Model获
2016-07-15 00:00
393 查看
摘要: 一步一步重写 CodeIgniter 框架 (7) —— Controller执行时将 Model获得的数据传入View中,实现MVC
1) 上一节讲述了 View 视图的加载过程,它是在 Loader 类中加载的,并通过 Include 语句进行包含。那么为了在 View 中传递变量,只需要在 include 语句所在环境的变量列表中加入这些变量即可。
2) 另外必须考虑到可以加载多个视图,所以还要保证在前面加载视图传入的变量,后面也可以访问。
注意必须定义成员变量 $_ci_cached_vars
2. 测试
修改 test_view.php ,在标题下输出 $info
那么相应在控制器函数中,也要改为
3. 测试
访问 http://localhost/learn-ci/index.php/welcome/hello
可以看到输出如下
1. 实现过程
1) 上一节讲述了 View 视图的加载过程,它是在 Loader 类中加载的,并通过 Include 语句进行包含。那么为了在 View 中传递变量,只需要在 include 语句所在环境的变量列表中加入这些变量即可。
2) 另外必须考虑到可以加载多个视图,所以还要保证在前面加载视图传入的变量,后面也可以访问。
// 非常重要 if (is_array($_ci_vars)) { $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars); } extract($this->_ci_cached_vars);
注意必须定义成员变量 $_ci_cached_vars
2. 测试
修改 test_view.php ,在标题下输出 $info
<html> <head> <title>My First View</title> </head> <body> <h1>Welcome, we finally met by MVC, my name is Zhangzhenyu!</h1> <p><?php echo $info ?></p> </body> </html>
那么相应在控制器函数中,也要改为
class welcome extends CI_Controller { function hello() { echo 'My first Php Framework!'; } function saysomething($str) { $this->load->model('test_model'); $info = $this->test_model->get_test_data(); $data['info'] = $info; $this->load->view('test_view', $data); } }
3. 测试
访问 http://localhost/learn-ci/index.php/welcome/hello
可以看到输出如下
Welcome, we finally met by MVC, my name is Zhangzhenyu!
People you want in our model is Zhangzhenyu相关文章推荐
- semantic-ui
- Smarty fetch方法
- 网站收藏
- maven搭建私服
- Java中的String类
- JAVA 内存管理总结
- mysql命令(二)--数据类型
- MathType公式编号的格式可以这样修改
- 这些输出格式是ChemDraw 15支持的吗
- 几何画板如何创建滑行反射变换
- 送给以后的自己…
- DeepFace: Closing the Gap to Human-Level Perform中文
- OpenBr官方教程-快速入门
- asp.net mvc code first实体关系一对一、一对多、多对多的设置方法
- 实现统计在线用户的几种方式,欢迎大家发表观点!
- thinkphp【错误改动大全】
- lnmp一键安装包
- java web中Excel文件导入
- Oracle存储过程(二)
- 简单配置搞定 Nginx + Tomcat + HTTPS