PHP-MVC evolution

1.3 MVC evolution

1.3.1 display merchandise

1, the data import products table

2, the upper class of a spoken MyPDO copied to the site, renamed MyPDO.class.php, this class file stored only MyPDO

3, create index.php in the site, code is as follows

<?php
//自动加载类
spl_autoload_register(function($class_name){
	require "./{$class_name}.class.php";
});
//连接数据库
$param=array(
	'user'	=>	'root',
	'pwd'	=>	'root'
);
$mypdo= MyPDO::getInstance($param);
//获取商品数据
$list=$mypdo->fetchAll('select * from products');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>
	<table border='1' width='980' bordercolor='#000'>
		<tr>
			<th>编号</th> <th>名称</th> <th>价格</th> <th>删除</th>
		</tr>
		<?php foreach($list as $rows):?>
		<tr>
			<td><?=$rows['proID']?></td>
			<td><?=$rows['proname']?></td>
			<td><?=$rows['proprice']?></td>
			<td><a href="">删除</a></td>
		</tr>
		<?php endforeach;?>
	</table>
</body>
</html>

operation result

Here Insert Picture Description

1.3.2 a Evolution: isolated view

1, pages created products_list.html (page view), the display portion of the copy of the code page to view the

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>
	<table border='1' width='980' bordercolor='#000'>
		<tr>
			<th>编号</th> <th>名称</th> <th>价格</th> <th>删除</th>
		</tr>
		<?php foreach($list as $rows):?>
		<tr>
			<td><?=$rows['proID']?></td>
			<td><?=$rows['proname']?></td>
			<td><?=$rows['proprice']?></td>
			<td><a href="">删除</a></td>
		</tr>
		<?php endforeach;?>
	</table>
</body>
</html>

2, load the view on the index.php page

<?php
//自动加载类
spl_autoload_register(function($class_name){
	require "./{$class_name}.class.php";
});
//连接数据库
$param=array(
	'user'	=>	'root',
	'pwd'	=>	'root'
);
$mypdo= MyPDO::getInstance($param);
//获取商品数据
$list=$mypdo->fetchAll('select * from products');
//加载视图
require './products_list.html';

1.3.3 Evolution of Two: Separation Model

Model Rules

1, a table corresponding to a model, consistent (mandatory), and the model name table

2, the model ends with Model (not required)

Code:

1, create a page at the site ProductsModel.class.php

<?php
//products模型用来操作products表
class ProductsModel {
	//获取products表的数据
	public function getList() {
		//连接数据库
		$param=array(
			'user'	=>	'root',
			'pwd'	=>	'root'
		);
		$mypdo= MyPDO::getInstance($param);
		//获取商品数据
		return $mypdo->fetchAll('select * from products');
	}
}

2, call the model the index.php page getList ()

<?php
//自动加载类
spl_autoload_register(function($class_name){
	require "./{$class_name}.class.php";
});
//实例化模型
$model=new ProductsModel();
$list=$model->getList();
//加载视图
require './products_list.html';

1.3.4 Evolution of three: the base model separation

The code for each model to be used to connect to the database, all we need to code the package to the base connection to the database model class (Model)

Here Insert Picture Description
Step 1: Create Model.class.php pages (base model) at the site

<?php
//基础模型
class Model {
	protected $mypdo;
	public function __construct() {
		$this->initMyPDO();
	}
	//连接数据库
	private function initMyPDO() {
		$param=array(
			'user'	=>	'root',
			'pwd'	=>	'root'
		);
		$this->mypdo= MyPDO::getInstance($param);
	}
}

Step two: ProductsModel inherit the base model classes

<?php
//products模型用来操作products表
class ProductsModel extends Model{
	//获取products表的数据
	public function getList() {
		return $this->mypdo->fetchAll('select * from products');
	}
}

1.3.5 Evolution IV: Separation controller

The controller code in the index.php page is unreasonable, because the project will be a lot of controllers, but only one index.php. It needs to be separated from the controller

Controller rules:

1, a block corresponding to a controller (required)

2, the controller end with Controller (not required)

3. A process controller to the end of Action (not required), the purpose of preventing method name is PHP keyword

Creating ProductsController.class.php

<?php
//商品模块
class ProductsController {
	//获取商品列表
	public function listAction() {
		//实例化模型
		$model=new ProductsModel();
		$list=$model->getList();
		//加载视图
		require './products_list.html';
	}
}

index.php page

<?php
//自动加载类
spl_autoload_register(function($class_name){
	require "./{$class_name}.class.php";
});
//确定路由
$c=$_GET['c']??'Products';   //控制器
$a=$_GET['a']??'list';		//方法
$c=ucfirst(strtolower($c));		//首字母大写
$a=strtolower($a);				//转成小写
$controller_name=$c.'Controller';	//拼接控制器类名
$action_name=$a.'Action';	//拼接方法名
//请求分发
$obj=new $controller_name();
$obj->$action_name();

Here Insert Picture Description

Addressed by passing parameters on the url.

c: Controller

a: Methods

Here Insert Picture Description

Note: Each request must enter from index.php. So called index.php file entry.

summary:

Here Insert Picture Description

Released 1921 original articles · won praise 2040 · Views 180,000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/105140988