MVC 是一种用于将应用程序的逻辑层和表现层分离出来的软件方法。在实践中,由于这种分离,所以你的页面中只包含很少的 PHP 脚本。
模型 代表你的数据结构。通常来说,模型类将包含帮助你对数据库进行增删改查的方法。
视图 是要展现给用户的信息。一个视图通常就是一个网页。
控制器 是模型、视图以及其他任何处理 HTTP 请求所必须的资源之间的中介,并生成网页。
自制一个简易的MVC模型。
1.入口文件index.php
唯一一个让浏览器直接请求的脚本文件
2.控制器controller
协调模型和视图
3.模型model
提供数据,保存数据
4.视图view
负责显示网页
5.动作action
控制器中的方法,用于被浏览器请求
--------------------文件夹结构-------------------------
主文件夹mvc_demo
--controllers
--ArticleController.php
--UserController.php
--models
--UserModel.php
--views
--User/index.php
index.php //入口文件
-----------------------------------------------------------
index.php
<?php
//这是入口文件
header("Content-Type:text/html;charset=utf-8");
//控制器
$c = $_GET['c'];
//包含控制器
include './controllers/'.$c.'Controller.php';
//实例化控制器对象
$className = $c.'Controller';
$controller = new $className();
//方法名
$a = $_GET['a'];
//调用方法
$controller->$a();<?php
class <strong>UserControl</strong>ler{
public function index(){
//echo "这是User控制器的index方法";
//包含文件并实例化一个模型
include './models/UserModel.php';
//通过模型获取数据
$model = new UserModel();
$str=$model->getUser();
include './views/User/index.php';
}
}<?php
class UserModel{
public function getUser(){
$str="helen";
return $str;
}
}<html> <head> </head> <body> <?php echo $str; ?> </body> </html>
以上就介绍了一个简易的MVC模型,包括了UserControl方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号