五.创建Model模块文件
我们已经能通过类似index.php?channel=photo&content=latest来识别访问的不同频道,但是我们希望controllers只是负责action的分发,具体读取和显示数据分别由models和views来实现,这样能有更好的扩展性。
我们在models下面新建一个photo.php文件用于读取图片信息,这个信息通常存储在数据库或者是文件中,为了简单化理清思路,我们先不管从何处得到数据,而是直接显示一张图片。
public function __construct() // 构造函数,创建对象时自动执行
echo("<img src='http://www.mosang.net/mvc/link.jpg'/>"); // 显示一张图片
<?php
class photo_Model
{
public function __construct() // 构造函数,创建对象时自动执行
{
echo("<img src='http://www.mosang.net/mvc/link.jpg'/>"); // 显示一张图片
}
}
?>
<?php
class photo_Model
{
public function __construct() // 构造函数,创建对象时自动执行
{
echo("<img src='http://www.mosang.net/mvc/link.jpg'/>"); // 显示一张图片
}
}
?>
下面更改controllers文件夹下的photo.php,我们不再希望photo.php直接输出信息,而是交给models下面的photo.php来实现。代码如下:
function showcurrent($channelname) {
$this->$channelname = $channelname;
$photosModel = new photo_Model($channelname);
删掉了 echo("您正在访问的频道是:".$channelname)这条语句。新增了创建图片模型类的语句
<?php
class photo_Controller {
var $channelname ;
function showcurrent($channelname) {
$this->$channelname = $channelname;
$photosModel = new photo_Model($channelname);
}
}
/*
删掉了 echo("您正在访问的频道是:".$channelname)这条语句。新增了创建图片模型类的语句
*/
?>
<?php
class photo_Controller {
var $channelname ;
function showcurrent($channelname) {
$this->$channelname = $channelname;
$photosModel = new photo_Model($channelname);
}
}
/*
删掉了 echo("您正在访问的频道是:".$channelname)这条语句。新增了创建图片模型类的语句
*/
?>
此时,我们输入地址会报错,因为还没有引入models下面新建一个photo.php文件。更改controllers目录下router.php,代码如下:
$channel = $_GET["channel"]; //获取频道名
$action = $_GET["action"];
$channelFile = SERVER_ROOT . '/controllers/' . $channel . ".php"; //频道名对应的php文件
$ModelFile = SERVER_ROOT . '/models/' . $channel . ".php"; //频道名对应的php文件
if (file_exists($ModelFile)) {
include_once ($ModelFile); //频道名对应的php文件引
if (file_exists($channelFile)) {
include_once ($channelFile); //频道名对应的php文件引入
$channelclass = $channel . "_Controller";
if (class_exists($channelclass)) {
$controller = new $channelclass; //创建实例
$controller->showcurrent($channel); //输出入当前频道名
<?php
$channel = $_GET["channel"]; //获取频道名
$action = $_GET["action"];
$channelFile = SERVER_ROOT . '/controllers/' . $channel . ".php"; //频道名对应的php文件
$ModelFile = SERVER_ROOT . '/models/' . $channel . ".php"; //频道名对应的php文件
if (file_exists($ModelFile)) {
include_once ($ModelFile); //频道名对应的php文件引
} else {
die("不存在该频道的Model文件!");
}
if (file_exists($channelFile)) {
include_once ($channelFile); //频道名对应的php文件引入
$channelclass = $channel . "_Controller";
if (class_exists($channelclass)) {
$controller = new $channelclass; //创建实例
$controller->showcurrent($channel); //输出入当前频道名
} else {
die("不存在该频道的类!");
}
} else {
die("不存在您请求的频道!");
}
?>
<?php
$channel = $_GET["channel"]; //获取频道名
$action = $_GET["action"];
$channelFile = SERVER_ROOT . '/controllers/' . $channel . ".php"; //频道名对应的php文件
$ModelFile = SERVER_ROOT . '/models/' . $channel . ".php"; //频道名对应的php文件
if (file_exists($ModelFile)) {
include_once ($ModelFile); //频道名对应的php文件引
} else {
die("不存在该频道的Model文件!");
}
if (file_exists($channelFile)) {
include_once ($channelFile); //频道名对应的php文件引入
$channelclass = $channel . "_Controller";
if (class_exists($channelclass)) {
$controller = new $channelclass; //创建实例
$controller->showcurrent($channel); //输出入当前频道名
} else {
die("不存在该频道的类!");
}
} else {
die("不存在您请求的频道!");
}
?>
现在在地址栏中输入域名+ index.php?channel=photo&content=latest,可以得到以下运行结果:

现在复制models下的photo.php文件,并放在同一目录下并更改文件名为news.php,更改news.php代码如下:
function __construct($channelname)
{ $this ->$channelname = $channelname;
echo($channelname."频道为您展示的内容:慕尼黑市计划再次投票是否从Linux迁回到Windows");
<?php
class news_Model
{ var $channelname;
function __construct($channelname)
{ $this ->$channelname = $channelname;
echo($channelname."频道为您展示的内容:慕尼黑市计划再次投票是否从Linux迁回到Windows");
}
}
?>
<?php
class news_Model
{ var $channelname;
function __construct($channelname)
{ $this ->$channelname = $channelname;
echo($channelname."频道为您展示的内容:慕尼黑市计划再次投票是否从Linux迁回到Windows");
}
}
?>
同时修改controllers文件夹下的news.php,代码如下:
function showcurrent($channelname) {
$this -> $channelname = $channelname;
$newsModel = new news_Model($channelname);
<?php
class news_Controller {
var $channelname;
function showcurrent($channelname) {
$this -> $channelname = $channelname;
$newsModel = new news_Model($channelname);
}
}
?>
<?php
class news_Controller {
var $channelname;
function showcurrent($channelname) {
$this -> $channelname = $channelname;
$newsModel = new news_Model($channelname);
}
}
?>
运行域名+ index.php?channel=news&content=latest,可以得到以下运行结果:
news频道为您展示的内容:慕尼黑市计划再次投票是否从Linux迁回到Windows
到目前为止,我们大致了解了controllers,models请求处理的分工。