Home >Backend Development >PHP Tutorial >Controller passes value to View in php

Controller passes value to View in php

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-08-18 09:16:131978browse

I want to write a small CMS using the MVC method. I have established a Controller, Model, and View, but I don’t know how to write the value from the Controller to the 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><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>

Reply content:

I want to write a small CMS using the MVC method, and have established a Controller, Model, and View, but I don’t know how to write the controller to pass values ​​to the 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><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>

First you have to specify the template in the controller, such as $this->display('test'); Then just include the template in the display method

If you want to be more complicated and add syntax sugar to the template, you can judge whether the template has compiled files in the display. If not, perform compilation (essentially regular replacement, such as replacing {$test} with $this-> test), and then include the compiled file

This way you can use the variables of the controller directly

I have written a simple mvc framework before. You can refer to it. The core content is at line 158 https://github.com/eyblog/mvc...

Save the variables and values ​​​​in the template into the data in the controller. File_get_content reads the content of the view file. The template variable identifier can be as you like, such as {$user} or {{user}}, and then the regular expression matches Replace, and finally enter echo
class Controller {
public $templateData = []; //Save the data mapping table of the template file
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>

}
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn