学习总结
- 使用MVC可以有效的把底层的数据库操作和前端的页面渲染分开
- 使用服务容器可以把模型和视图封装在服务容器中,减少对外部对象的依赖
- 门面技术,可以把模型和视图中的普通方法封装成静态方法,方便客户端调用
- 路由解析可以把url地址中的控制器,方法,参数进行有效解析
效果展示
1.模型类 Model.php
<?php
namespace nMVC
{
use PDO;
class Model
{
public function getData():array
{
$dbConn = new \PDO('mysql:host=localhost;dbname=db_phpstudy','root','root');
$res = $dbConn->query('SELECT * FROM `tb_goods` LIMIT 10')->fetchAll(\PDO::FETCH_ASSOC);
return $res;
}
}
}
?>
2.视图类 View.php
<?php
namespace nMVC
{
class View
{
public function showData($records)
{
echo '<style>
* {
padding: 0px;
margin: 0px;
box-sizing: border-box;
}
.show
{
width: 600px;
height: 500px;
border: 1px solid #555;
margin-left: auto;
margin-right: auto;
margin-top:20px;
background-color: lightseagreen;
display: flex;
flex-flow: column nowrap;
justify-content: space-evenly;
align-items: center;
}
.show>div
{
width: 100%;
border-bottom: 1px solid white;
display: flex;
flex-flow: row nowrap;
justify-content: space-evenly;
align-items: center;
}
.show>div:first-of-type
{
font-size: larger;
font-weight: bolder;
}
.show>div>span
{
text-align: center;
}
.show > div > span:nth-of-type(1) {
width: 50px;
}
.show > div > span:nth-of-type(2) {
width: 150px;
}
.show > div > span:nth-of-type(3) {
width: 150px;
}
.show > div > span:nth-of-type(4) {
width: 100px;
}
</style>';
echo '<div class="show">';
echo '<h2>商品信息展示</h2>';
echo '<div><span>编号</span><span>名称</span><span>价格</span><span>时间</span></div>';
foreach($records as $record)
{
echo '<div >';
echo '<span>',"{$record['id']}",'</span>';
echo '<span>',"{$record['name']}",'</span>';
echo '<span>',"{$record['price']}\\{$record['unit']}",'</span>';
echo '<span>',"{$record['sdate']}",'</span>';
echo '</div>';
}
echo '</div>';
}
}
}
?>
3.服务容器类 Container.php
<?php
namespace nMVC
{
require 'Model.php';
require 'View.php';
class Container
{
protected $case = [];
public function bind($alias,\Closure $proce)
{
$this->case[$alias] = $proce;
}
public function make($alias)
{
return call_user_func_array($this->case[$alias],[]);
}
public function delete($alias)
{
unset($this->case[$alias]);
}
}
}
?>
4.门面类 Facede.php
<?php
namespace nMVC
{
require 'Container.php';
class Facede
{
protected static $container = null;
protected static $data = [];
public static function init($container)
{
static::$container = $container;
}
}
class Model1 extends Facede
{
public static function getdata()
{
static::$data = static::$container->make('model')->getdata();
}
}
class View1 extends Facede
{
public static function showdata()
{
$records = static::$container->make('view')->showdata(static::$data);
return $records;
}
}
}
?>
5.控制器类 Controller4.php
<?php
namespace nMVC
{
require 'Facede.php';
class Controller4
{
public function __construct(Container $container)
{
Facede::init($container);
}
public function index()
{
Model1::getdata();
return View1::showdata();
}
}
echo 'controller4 Facede 门面技术<br>';
//创建一个服务容器的对象
$container = new Container();
//服务容器绑定数据
$container->bind('model',function(){ return new Model; });
$container->bind('view',function(){ return new View; });
//创建一个控制器
$controller4 = new Controller4($container);
echo $controller4->index();
//对象销毁
$container->delete('model');
$container->delete('view');
}
?>
6.路由解析Route.php
<?php
class GoodsController
{
public function showdata($id,$name)
{
echo '商品编号:',$id,'<br>';
echo '商品名称:',$name,'<br>';
}
}
//http://localhost/php11/0514/Route.php/goods/showdata/id/1/name/%E7%99%BD%E8%8F%9C/sdate
//获取pathinfo信息
$pathInfoStr = $_SERVER['PATH_INFO'];
//过虑空数组元素,生成一个数组
$pathInfoArr = array_values(array_filter(explode('/',$pathInfoStr)));
//获取要访问的控制器
$controller =ucfirst(array_shift($pathInfoArr)) .'Controller';
//获取要访问的控制器的方法
$method = array_shift($pathInfoArr);
//获取参数
$paras = [];
for($i=0;$i<count($pathInfoArr);$i+=2):
//如果参数没有值就不解析这个参数
if(isset($pathInfoArr[$i+1])):
$paras[$pathInfoArr[$i]] = $pathInfoArr[$i+1];
endif;
endfor;
//动态生成一个控制器类,动态调用控制器类中的方法
$goods = new $controller();
$goods->$method(...array_values($paras));
?>
效果展示