作者:200355人 | 来源:互联网 | 2020-08-12 04:53
一、 MVC模式流程图
命名规则:test(名字)Controller(控制器文件).class.php ( 类文件 )
get();//模型按照指令取数据
//按指令选择视图 实例化一个view的对象
$testView = new testView();
//把取到的数据按用户的样子显示出来
$testView -> display($data);
}
}
?>
2. testModel.class.php 模型类文件
命名规则:test(模型文件名称 )Model( 模型文件).class.php 类文件
3. testView.class.php 视图类文件
4. 单一入口文件
让他来调用控制器,而控制器去调用模型和视图
show();//调用方法
?>
5.运行结果
四、简单的MVC实例改进----方法封装
1. 封装一个实例化控制器等的对象和调用方法的函数
show();
eval('$obj = new '.$name.'Controller();$obj->'.$method.'();');//把字符串转换为可执行的php语句
}
//封装一个实例化模型的对象和调用方法的函数
function M($name){
require_once('/libs/Model/'.$name.'Model.class.php');
//$testModel = new testModel();
eval('$obj = new '.$name.'Model();');//实例化
return $obj;
}
//封装一个实例化视图的对象和调用方法的函数
function V($name){
require_once('/libs/View/'.$name.'View.class.php');
//$testView = new testView();
eval('$obj = new '.$name.'View();');
return $obj;
}
//为了安全性 ,过滤函数
//addslashes对’,字符进行转义
//get_magic_quotes_gpc()当前魔法符号的打开状态,打开返回true,
function daddslashes($str){
return (!get_magic_quotes_gpc() )? addslashes($str) : $str;
}
?>
2.重新编写入口文件index.php
浏览器url访问形式 http://......index.php?cOntroller=控制器名&method=方法名
3.运行结果
浏览器访问 http://localhost:8080/MVC/index.php?cOntroller=test&method=show 显示hello world
想了解更多PHP相关问题请访问PHP中文网:PHP视频教程
以上就是PHP——MVC模式讲解与实例的详细内容,更多请关注 第一PHP社区 其它相关文章!