六祎-PHP搭建MVC架构

mac2022-06-30  85

搭建MVC结构(使用MVC设计模式搭建的框架)

MVC: M(model模型) V(view视图) C(control控制)本MVC模式采用的是单一入口:如:http://localhost/test/mvc/index.php?m=stu&a=add //打开学生信息的添加界面其中 m 的值  stu  表示访问的是stucontroller控制器a的值add表示是访问 Stucontroller 的 add 方法 index.php?m=order&a=del&id=100

通过请求访问了控制层C,C调度M模型获取我们所需的信息数据,然后再去加载V视图将结果整合后响应给我们

使用MVC设计模式搭建的项目结构叫框架

MVC是一个框架模式,它强制性的使应用程序的输入、处理和输出分开

创建目录: ORG 第三方扩展类 model M(模型)层目录(M) controller C(控制)层目录(C) view V(视图) 层目录(smarty的模板目录) public 公共资源目录 libs Smarty库(解压到这里即可) view_c Smarty模板编译目录(可选) cache Smarty静态缓存目录(可选) configs 配置文件目录将自己写好的model类放到model目录下 model/model.class.php在ORG目录下创建一个tp1.class.php的smarty子类,用于初始化smarty 代码如下:

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // Smarty信息的初始化类 class Tpl extends Smarty{ public function __construct(){ parent::__construct(); // 构造父类 // 初始化Smarty对象中属性: $this->template_dir = "view"; // smarty模板目录 $this->compile_dir = "view_c"; // smarty模板编译目录 $this->config_dir = "configs"; // smarty配置文件目录 $this->cache_dir = "cache"; // smarty模板静态缓存目录 // $this->caching = true; // 是否开启静态缓存 // $this->cache_lifetime = 3600; // 静态缓存时间(秒) // 指定定界符 $this->left_delimiter="{"; // 左定界符 $this->right_delimiter="}"; // 右定界符 } } 在ontroller目录下创建Controller类,继承Tp1类,文件名叫:controller.class.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // Controller的控制基类 class Controller extends Tpl{ public function __construct(){ parent::__construct(); } /** *Controller初始化方法(在这个方法里根据参数a的值决定调用对应的方法) * */ public function init(){ // 获取a参数的值 $a = isset($_GET["a"])?$_GET["a"]:"index"; // 默认值为index // 判断当前Controller是否存在此方法 if(method_exists($this,$a)){ // 调用此方法 $this->$a(); }else{ die("没有找到{$a}对应的方法"); } } } 在最外层,创建一个  index.php  的入口文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <?php // 网站的主入口程序 // 自动加载类 function __autoload($name){ $name = strtolower($name); // 转成小写 if(file_exists("./controller/{$name}.class.php")){ require("./controller/{$name}.class.php"); }elseif(file_exists("./model/{$name}.class.php")){ require("./model/{$name}.class.php"); }elseif(file_exists("./ORG/{$name}.class.php")){ require("./ORG/{$name}.class.php"); }elseif(file_exists("./libs/".ucfirst($name).".class.php")){ require("./libs/".ucfirst($name).".class.php"); }elseif(file_exists("./libs/sysplugins/{$name}.php")){ require("./libs/sysplugins/{$name}.php"); }else{ die("错误:没有找到对应{$name}类!"); } } // 数据连接配置文件 require("./configs/config.php"); // 获取参数m的值,并创建对应的Controller对象 $mod = isset($_GET['m'])?$_GET['m']:"index"; // 拼装Controller类名 $classname = ucfirst($mod)."Controller"; // 创建对应的Controller对象 $controller = new $classname(); // 执行Controller的初始化(Controller入口) $controller->init(); 在 configs  的目录下创建一个config.php的配置文件测试 7.1. 在Controller 目录下创建一个indexcontroller.class.phpass.php文件

 

1 2 3 4 5 6 7 8 // 网站入口的主Controller类 class IndexController extends Controller{ // 默认入口方法 public function index(){ $this->assign("title","MVC的测试界面"); $this->display("index.html"); } }

7.2. 在view目录下创建index.html模板页面

1 2 3 4 5 6 7 8 9 <!DOCTYPE html> <html> <head> <title>{$title}</title> </head> <body> <h2>{$title}</h2> </body> </html>

 

最新回复(0)