一步一步重写 CodeIgniter 框架 (7) —— Controller执行时将 Model获

 

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

 

转载于:https://my.oschina.net/yomut/blog/712606

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值