Blogger Information
Blog 23
fans 0
comment 0
visits 19752
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
根据课堂案例,练习一下mvc的实现原理--1031
风吹的博客
Original
726 people have browsed it

index.php

实例

<?php
$server = $_SERVER;
//$request_uri=$_SERVER['REQUEST_URI']; // /index2.php/home/index

//echo '<pre>';
//print_r($server);
//此时我们认为我们要访问的是控制器中的方法
$path_info = $_SERVER['PATH_INFO'];
//去掉$path_info中首位的/
$path = ltrim($path_info,'/');
//以/为分隔将$path分割为数组
$controller_method = explode('/', $path);
//将首字母大写
$controller_method[0]=ucfirst($controller_method[0]);
//引用控制器中的脚本
require_once  __DIR__.'/controller/'.$controller_method[0].'.php';

$controller = $controller_method[0];///Home
$method = $controller_method[1];//index
$obj = new $controller();
include __DIR__.'/model/model.php';

$res = $obj->$method($c,$d);
include __DIR__.'/view/v.php';

运行实例 »

点击 "运行实例" 按钮查看在线实例

控制器controller。Home.php

实例

<?php
class Home{
	
	function index($a,$b){
    return  $a+$b;
	}

}

运行实例 »

点击 "运行实例" 按钮查看在线实例

视图view。v.php

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>view</title>
</head>
<body>
<h2 style="color: red"><?php echo $res;?></h2>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

模型model。model.php

实例

<?php
$c = 111111111;
$d = 222222222;
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

QQ拼音截图20191123151207.png

一开始写出来了但是没有model,然后我就在想如何加进去一个model,然后我在这里就决定把model当做从数据库中引用的数据,然后传值运算,渲染

这次写的MVC和前几次相比,主要差别还是在index.php上。进行了路由解析,然后拿取PATH_INFO。文件名首字母大写。拼接Home.php这个文件名。

Correcting teacher:查无此人查无此人

Correction status:qualified

Teacher's comments:完成的不错,继续加油
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!