一个简略的php MVC实例
一个简单的php MVC实例
原文 http://jcodecraeer.com/a/phpjiaocheng/2012/0305/10.html
?
这个小程序一共包含6个文件,其中index.php是程序入口、post.htm是留言表单、在lib文件夹里Model、View 、Controller三个文件分别实现MVC,DataAccess是一个简单的数据库访问类。其实这个程序是国外的一个人写的。
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<?php
/**
* 一个用来访问MySQL的类
* 仅仅实现演示所需的基本功能,没有容错等
* 代码未作修改,只是把注释翻译一下,加了点自己的体会
*/
class DataAccess {
var $db; //用于存储数据库连接
var $query; //用于存储查询源
//! 构造函数.
/**
* 创建一个新的DataAccess对象
* @param $host 数据库服务器名称
* @param $user 数据库服务器用户名
* @param $pass 密码
* @param $db 数据库名称
*/
function __construct($host,$user,$pass,$db) {
$ this ->db=mysql_pconnect($host,$user,$pass); //连接数据库服务器
mysql_select_db($db,$ this ->db); //选择所需数据库
//特别注意$db和$this->db的区别
//前者是构造函数参数
//后者是类的数据成员
}
//! 执行SQL语句
/**
* 执行SQL语句,获取一个查询源并存储在数据成员$query中
* @param $sql 被执行的SQL语句字符串
* @return void
*/
function fetch($sql) {
$ this ->query=mysql_unbuffered_query($sql,$ this ->db); // Perform query here
}
//! 获取一条记录
/**
* 以数组形式返回查询结果的一行记录,通过循环调用该函数可遍历全部记录
* @return mixed
*/
function getRow () {
if ( $row=mysql_fetch_array($ this ->query,MYSQL_ASSOC) )
//MYSQL_ASSOC参数决定了数组键名用字段名表示
return $row;
else
return false ;
}
}
?>
|
?
下面再来介绍一下Model类。
这个类也很简单,里面的函数一看就知道,是针对各种数据操作的,它通过DataAccess访问数据库。
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<?php
//! Model类
/**
* 它的主要部分是对应于留言本各种数据操作的函数
* 如:留言数据的显示、插入、删除等
*/
class Model {
var $dao; //DataAccess类的一个实例(对象)
//! 构造函数
/**
* 构造一个新的Model对象
* @param $dao是一个DataAccess对象
* 该参数以地址传递(&$dao)的形式传给Model
* 并保存在Model的成员变量$this->dao中
* Model通过调用$this->dao的fetch方法执行所需的SQL语句
*/
function __construct(&$dao) {
$ this ->dao=$dao;
}
function listNote() { //获取全部留言
$ this ->dao->fetch( "SELECT * FROM note" );
}
function postNote($name,$content) { //插入一条新留言
$sql = "INSERT INTO `test`.`note`
(`id`, `name`, `content`, `ndate`, `add`)
VALUES (NULL, '$name', '$content', NULL, NULL);" ;
//echo $sql; //对于较复杂的合成SQL语句,<br>
//调试时用echo输出一下看看是否正确是一种常用的调试技巧
$ this ->dao->fetch($sql);
}
function deleteNote($id) { //删除一条留言,$id是该条留言的id
$sql = "DELETE FROM `test`.`note` WHERE `id`=$id;" ;
//echo $sql;
$ this ->dao->fetch($sql);
}
function getNote() { //获取以数组形式存储的一条留言
//View利用此方法从查询结果中读出数据并显示
if ( $note=$ this ->dao->getRow() )
return $note;
else
return false ;
}
}
?>
|
?
看完这两个类之后你可能会发现这与以前我们写程序差不多,的确现在还闻不到MVC的味道,如果你不懂MVC,在这两个类的基础上你完全可以开始写你以前的程序了。例如要显示全部留言,只需要写入下代码:
PHP代码:
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
require_once( 'lib/DataAccess.php' );
require_once( 'lib/Model.php' );
$dao=& new DataAccess ( 'localhost' , 'root' , '' , 'test' );
$model=& new Model($dao);
$model->listNote();
while ($note=$model->getNote())
{
$output.= "姓名:$note[name]<br> 留言:<br> $note[content] <br> <hr>" ;
}
echo $output;
?>
|
?
很亲切吧,呵呵。
有了这个“感情基础”你就不会对MVC望而生畏了,下面我们就要上今天的主菜了,那就是“Controller”闪亮登场!
先大体浏览一下主要结构,它包括一个Controller类以及派生出的三个子类(listController对应显示留言功能、postController对应发表留言功能以及deleteController对应删除留言功能)。
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<?php
//! Controller
/**
* 控制器将$_GET['action']中不同的参数(list、post、delete)
* 对应于完成该功能控制的相应子类
*/
class Controller {
var $model; // Model 对象
var $view; // View 对象
//! 构造函数
/**
* 构造一个Model对象存储于成员变量$this->model;
*/
function __construct (& $dao) {
$ this ->model=& new Model($dao);
}
function getView() { //获取View函数
//返回视图对象view
//对应特定功能的Controller子类生成对应的View子类的对象
//通过该函数返回给外部调用者
return $ this ->view;
}
}
//用于控制显示留言列表的子类
class listController extends Controller{ //extends表示继承
function __construct (& $dao) {
parent::__construct($dao); //继承其父类的构造函数
//该行的含义可以简单理解为:
//将其父类的构造函数代码复制过来
$ this ->view=& new listView($ this ->model);
//创建相应的View子类的对象来完成显示
//把model对象传给View子类供其获取数据
}
}
//用于控制添加留言的子类
class postController extends Controller{
function __construct (& $dao, $post) {
parent::__construct($dao);
$ this ->view=& new postView($ this ->model, $post);
//$post的实参为$_POST数组
//表单中的留言项目存储在该系统数组中
}
}
//用于控制删除留言的子类
class deleteController extends Controller{
function __construct (& $dao, $id) {
parent::__construct($dao);
$ this ->view=& new deleteView($ this ->model, $id);
}
}
?>
|
?
大体浏览之后,你一定打算开始仔细研究它了吧,别急,为了心中有数,我们先从宏观着眼, 先看看总入口index.php是如何调用Controller的:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<meta http-equiv="</code">
<code class="js string">"Content-Type" content= "text/html; charset=gb2312" />
<title>PHP MVC留言板</title>
<a href="</code"><code class="js string">"post.htm" >添加新留言 <p> </p>
php
//!index.php 总入口
/**
* index.php的 调用形式为:
* 显示所有留言:index.php?action=list
* 添加留 言 :index.php?action=post
* 删除留言 :index.php?action=delete& id=x
*/
require_once( 'lib/DataAccess.php' );
require_once( 'lib/Model.php' );
require_once( 'lib/View.php' );
require_once( 'lib/Controller.php' );
//创建DataAccess对象(请根据你的需要修改参数值)
$dao=& new DataAccess ( 'localhost' , 'root' , '' , 'test' );
//根据$_GET["action"]取值的不同调用不同的控制器子类
$action=$_GET[ "action" ];
switch (
$action)
{
case "post" :
$controller=& new postController($dao,$_POST); break ;
case "list" :
$controller=& new listController($dao); break ;
case "delete" :
$controller=& new deleteController($dao,$_GET[ "id" ]); break ;
default :
$controller=& new listController($dao); break ; //默认为显示留言
}
$view=$controller->getView(); //获取视图对象
$view->display(); //输出HTML
?>
|
?
看过index.php之后你就更清楚了吧,原来功能是通过$_GET[“action”]指定的,由一个switch结构分发,不同的功能对应不
同的Controller子类。现在可以滚上去(滚动页面上去的简称,绝非不洁用语^_^)仔细看看这个Controller代码了。注释应该很细了,不
懂的地方就去看看PHP5的OOP语法和概念吧,单纯看这些概念总是越看催眠效果越好,现在带着实际问题去看,应该有所不同吧。不过我还是建议你在完成这
个MVC的Hello World知道MVC是怎么回事之后下功夫打好OOP的基础,毕竟那是根本啊。
怎么样,Controller真是个光说
不练的家伙吧,看不到三行它就把你引向View了,那就看看View吧。
View里有对应的子类,负责相应功能的显示。理解了
Controller,View的代码就不难看了,难看的话也是因为混杂着HTML的原因,它所做的就是从Model获取所需的数据,然后塞到HTML 中。
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
<?php
//! View 类
/**
* 针对各个功能(list、post、delete)的各种 View子类
* 被Controller调用,完成不同功能的网页显示
*/
class View {
var
$model; //Model对象
var $output; //用于保存输出HTML代码 的字符串
//! 构造函数
/**
* 将参数中的Model对象接收并存储在成员变量$this->model中
* 供子类通 过model对象获取数据
*/
function __construct (&$model) {
$ this ->model=$model;
}
function
display() { //输出最终格式化的HTML数据
echo($ this ->output);
}
}
class
listView extends View //显示所有留言 的子类
{
function __construct(&$model)
{
parent::__construct(&$model); //继承父类的构造函数(详见Controller)
$ this ->model->listNote();
while ($note=$ this ->model->getNote()) //逐行获取数据
{
$ this ->output.= "姓名:$note[name]<br> 留 言:<br> $note[content]
<a href="%5C%22%22</code"><code class="js plain">.$_SERVER[ 'PHP_SELF' ]. "?action=delete& amp;id=$note[id]\">删除 " ;
}
}
}
class
postView extends View //发表留言的子类
{
function __construct(&$model, $post)
{
parent::__construct(&$model);
$ this ->model->postNote($post[name],$post[content]);
$ this ->output= "Note Post OK! 'PHP_SELF' ]. "?action=list\">查看 ";
}
}
class
deleteView extends View //删除留言的子类
{
function __construct(&$model, $id)
{
parent::__construct(&$model);
$ this ->model->deleteNote($id);
$ this ->output= "Note Delete OK! 'PHP_SELF' ]. "?action=list\">查看 ";
}
}
?>
|
之所以UI方面写得如此简陋,是因为这些工作可以交给Smarty这样的模板去做,而我们这里就像集中精力研究MVC,不想把Smarty扯进来,
所以就这样凑合了,以后我们可以再把Smarty结合进来。
看了这个东西之后不知你是否对MVC的概念和实现更明白了一点。
我也是个初学
者,这是个依葫芦画瓢之作,目的就是想了解一下MVC,如果你是高手,我很想得到你的点评,这样的划分和架构是否符合MVC的理念?还有哪些应该改进之
处?
当然,大家都知道现在很多关于MVC的争论,这很正常,就像关于开发语言的争论一样,永无休止,学术上的争论有助于创新。作为我们学技术、用 技术而言,一
定要踏实深入学习,掌握了基本用法之后再去讨论,那才是更高层次的发展,在自己都搞不清的情况下在哪里争论只能是浪费时间。
下面说
说我体会到的MVC的好处,它的确给程序的功能扩展带来方便,比如这个例子我们想要增加一个根据用户名查询留言的功能,只需要在Model里增加一
个查询函数(突然发现这些函数的用法很像存储过程),Controller和View里增加相应的子类,这种分离带来的好处是程序功能模块可以即插即用,
再就是整个程序的逻辑非常清晰。我想,对于需求变动频繁的Web应用来说,这种特性也许是很有价值的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

When a SpringBoot novice creates a project, the Controller cannot be scanned for a series of problems 1.2.3.4.5.6. Another way is to add @ComponentScan(basePackages={"xxx.xxx.xx","xxx.xxx" when starting the service class) .xx”}) is the fully qualified name of the package, which can be used for multiple SpringBoot custom controllers. The SpringBoot custom controller route cannot be scanned and cannot be found because the startup class and the custom Controller package are not in the same directory. Officially recommended placement of application.java

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Preface In some cases, the prefixes in the service controller are consistent. For example, the prefix of all URLs is /context-path/api/v1, and a unified prefix needs to be added to some URLs. The conceivable solution is to modify the context-path of the service and add api/v1 to the context-path. Modifying the global prefix can solve the above problem, but there are disadvantages. If the URL has multiple prefixes, for example, some URLs require prefixes. If it is api/v2, it cannot be distinguished. If you do not want to add api/v1 to some static resources in the service, it cannot be distinguished. The following uses custom annotations to uniformly add certain URL prefixes. one,

After problems occurred in many centralized exchanges, more and more cryptocurrency investors began to transfer assets to cold wallets to reduce the risks posed by centralized exchanges. This article will introduce Trezor, the world's earliest cold wallet provider. Since the first cold wallet was launched in 2014, it has been sold in many countries around the world. Trezor's products include Model One launched in 2014 and the advanced version Model T launched in 2018. The following will continue to introduce the differences between these two products and other cold wallets. What is Trezor cold wallet? In 2014, Trezor launched the first cold wallet ModelOne. In addition to common BTC, ETH, USDT and other currencies, the wallet also supports more than 1,000 other currencies.

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table
