在php中Controller向View传值

php中文网
发布: 2016-08-18 09:16:13
原创
2244人浏览过

想用mvc的方式写一个小的cms,建立了controller、model、view,但是不知道controller向view传值应该怎么写?

index.php 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><html>
<head>
    <meta charset="UTF-8">
    <title>BUG列表</title>
</head>
<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>
</body>
</html>
</code>
登录后复制

回复内容:

想用mvc的方式写一个小的cms,建立了controller、model、view,但是不知道controller向view传值应该怎么写?

index.php testController.class.php
<code>class testController{
    function show(){
        $testModel = new testModel();
        $data = $testModel->get();
        return $data;
    }
}
</code>
登录后复制

testModel.class.php

立即学习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><html>
<head>
    <meta charset="UTF-8">
    <title>BUG列表</title>
</head>
<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>
</body>
</html>
</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>
登录后复制

}
}

相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号