- <?php
- // mvc pattern 单点控制
- // 简单的控制器 C
- class NumberController extends DefaultController
- {
- public $model = null;
- public function __construct()
- {
- $this->model = NumberModel();
- }
- // V
- public function view($value=0)
- {
- echo "The square of this number is: ",$this->model->square($value);
- }
- }
- // 控制器的基类
- class DefaultController
- {
- public function run($action = 'index', $id = 0)
- {
- if (!method_exists($this, $action)) {
- $action = 'index';
- }
- return $this->$action($id);
- }
- // 简单输出模版
- public function index()
- {
- $html = "";
- for($i = 1; $i < 10; $i++)
- {
- $html .= sprintf('<li><a href="index.php?a=view&m=number&id=%d"></a></li>', $i, "INDEX_$i");
- }
- echo sprintf("<ul>%s</ul>", $html);
- }
- }
- // 一个非常简单的模型,计算四边形的面积 M
- class NumberModel
- {
- public function square($number)
- {
- return $number * $number;
- }
- }
- // 获取控制器,模型,和参数
- $action = isset($_GET['a']) ? $_GET['a'] : 'index';
- $module = isset($_GET['m']) ? $_GET['m'] : '';
- $id = isset($_GET['id']) ? $_GET['id'] : '';
- // 找到我们的控制器
- switch($module)
- {
- case 'number':
- $controller = new NumberController();
- break;
- default:
- $controller = new DefaultController();
- break;
- }
- //<li><a href="index.php?a=view&m=number&id=1">Index_1</a></li>
- $controller->run($action, $id);
- //该片段来自于http://www.codesnippet.cn/detail/1004201512182.html
来源: http://www.codesnippet.cn/detail/1004201512182.html