首先,约定请求页面时的URL,以如下结构进行实现:localhost/index.php?c=demo&a=index¶m=welcome
目录结构
/*
├─www # 网站根目录
│ ├─controller # 控制器目录
│ │ ├─democontroller.php # demo控制器
│ ├─model # 模型目录
│ │ ├─model.php # model模型
│ ├─view # 视图目录
│ │ ├─index.php # index视图
│ ├─index.php # 入口文件
*/
源码:
入口文件:index.php
//index.php
// get runtime controller prefix
$c_str = $_GET['c'];
// the full name of controller
$c_name = $c_str.'controller';
// the path of controller
$c_path = 'controller/'.$c_name.'.php';
// get runtime action
$method = $_GET['a'];
// get runtime parameter
$param = $_GET['param'];
// load controller file
require($c_path);
// instantiate controller
$controller = new $c_name;
// run the controller method
$controller->$method($param);
// End of index.php
控制器: controller/democontroller.php
// controller/democontroller.php
class DemoController
{
// private $data = 'Hello furzoom!';
function index($param)
{
// echo 'hello world';
require('view/index.php');
require('model/model.php');
$model = new Model();
$view = new Index();
$data = $model->getData($param);
$view->display($data);
}
}// End of the class DemoController
// End of file democontroller.php
模型: model/model.php
// model/model.php
class Model {
private $data = array(
'title' => 'Hello furzoom',
'welcome' => 'Welcome to furzoom.com',
);
public function getData($key) {
return $this->data[$key];
}
}
// End of model.php
视图:view/index.php
// view/index.php
class Index {
public function display($output) {
// ob_start();
echo $output;
}
}
// End of index.php
参考至乔晓峰的博客https://www.cnblogs.com/foonsun/p/5788564.html