MVC:
M:model
V:view
C:controller
文件分类:
index.php文件代码:
<?php //phpinfo(); // 路由解析 $server = $_SERVER; $path_info = $_SERVER['PATH_INFO']; // /home/index //$script_name = $_SERVER['script_name']; //脚本名称 /index.php //$request_uri = $_SERVER['request_uri']; // /index.php/home/index $path = ltrim($path_info,'/'); //去除字符串左边的字符 //分解$path为数组 controller_method $controller_method = explode('/', $path); $controller_method[0] = ucfirst($controller_method[0]); //首个字符大写 $controller = $controller_method[0]; $method = $controller_method[1]; //加载类 require_once __DIR__ . '/controller/' . $controller_method[0] . '.php'; $obj = new $controller(); $res = $obj->$method(); exit($res);
controller:home.php代码
<?php /** * */ class Home { public function index(){ echo 'hello MVC'; } //调用视图方法 public function welcome(){ //echo 'welcome China'; require_once __DIR__ . '/../view/welcome.php'; } //调用model方法 public function Newsmodel(){ require_once __DIR__ . '/../model/Newsmodel.php'; } }
View:welcome.php代码
<!DOCTYPE html> <html> <head> <title>Welcome</title> </head> <body> <div style="font-size: 18px;text-align: center; color:red;">welcome China</div> <?php echo date('Y-m-d H:i:s')?> </body> </html>
model:newsmodel.php代码:
<!DOCTYPE html> <html> <head> <title>Newsmodel</title> </head> <body> <div style="font-size: 18px;text-align: center; color:red;">Newsmodel</div> <?php echo date('Y-m-d H:i:s')?> </body> </html>
运行结果截图:
总结理解截图: