php打造属于自己的MVC框架

2019-04-08 15:25:54刘景俊

我们在前面的controller中只是输出了一个“hello world”,并没有达到mvc的效果,下面我将在此基础上增加视图功能,相信到这里大家基本已经能想到如何添加视图功能了。对,就是通过万恶的require或者include来实现。
首先我们在view文件夹下建立一个index.php,随便写点什么(呵呵,我写的还是hello world)。之后我们改写一下我们之前的DemoController。代码如下:

<?php
class DemoController
{
function index()
{
require('view/index.php');
}
}
/* End of file democontroller.php */

再在浏览器中运行一下,看看是不是已经输出了我们想要的内容了。
接着我们通过controller向view传递一些数据看看,代码如下:

<?php
class DemoController
{
function index()
{
$data['title']='First Title';
$data['list']=array('A','B','C','D');
require('view/index.php');
}
}
/* End of file democontroller.php */

view文件夹下index.php文件代码如下:

<html>
<head>
<title>demo</title>
</head>
<body>
<h1><?php echo $data['title'];?></h1>
<?php
foreach ($data['list'] as $item)
{
echo $item.'<br>';
}
?>
</body>
</html>
相关文章 大家在看