一个简单的 MVC 路由索引
说明:点击页面,判断get值 调用不同的类方法返回值。
分配器 1031.01.php
<?phpinclude __DIR__ . '/controller/config.php';include __DIR__ . '/Views/index.php';
controller/config.php
<?php include __DIR__ . '/../Model/function.php'; // 分配路由 if (empty($_GET)) { $_GET = ['home' => ""];}if (!empty($_GET)) { $a = array_keys($_GET)[0]; $b = index::$a($a);}
model/function.php
<?php class Index{ public static function __callStatic($name, $arguments = []) { return "路由是" . $name; }}
view/index.php
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>20191031MVC</title></head> <body> <div class="pp"> <ul> <li><a href="?home"><span>首页</span></a></li> <li><a href="?news"><span>新闻</span></a></li> <li><a href="?produt"><span>产品</span></a></li> <li><a href="?service"><span>服务支持</span></a></li> <li><a href="?about"><span>关于我们</span></a></li> </ul> </div> <div class="result"> <span><?php echo $b; ?> </span> </div></body> </html>
效果图