1.MVC (一种架构模式)
使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式。
M: Model(模型层),最bottom一层,是核心数据层,程序需要操作的数据或信息.
V:View (视图层),最top一层,直接面向最终用户,视图层提供操作页面给用户,被誉为程序的外壳.
C: Controller(控制层),是middile层, 它负责根据用户从”视图层”输入的指令,选取”数据层”中的数据,然后对其进行相应的操作,产生最终结果。
模型层-model.php
<?php
class Model
{
public function getData()
{
$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=user';
$username = 'root';
$password = 'root123';
$pdo = new PDO($dsn, $username, $password);
$sql = "select `username`,`sex` from `users` order by `id` asc limit 5";
$stmt = $pdo->query($sql);
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $res;
}
}
视图层-view.php
<?php
class Model
{
public function getData()
{
$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=user';
$username = 'root';
$password = 'root123';
$pdo = new PDO($dsn, $username, $password);
$sql = "select `username`,`sex` from `users` order by `id` asc limit 5";
$stmt = $pdo->query($sql);
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $res;
}
}
视图层-view.php
<?php
class View
{
public function fetch($data)
{
$html = <<<EOF
<table>
<caption>用户信息表</caption>
<thead>
<tr>
<td>用户名</td>
<td>性别</td>
</tr>
</thead>
<tbody>
EOF;
foreach ($data as $user) {
$html .= "
<tr>
<td>{$user['username']}</td>
<td>{$user['sex']}</td>
</tr>";
}
$html .= " </tbody>
</table>";
return $html;
}
}
控制层-controller.php
<?php
require 'auto_load.php';
class Controller
{
public function fetch()
{
$data = (new Model())->getData();
echo (new View())->fetch($data);
}
}
// 客户端
(new Controller())->fetch();
2.容器与依赖注入
依赖注入是通过 php 的映射函数,解析到类在实例化的时候所依赖的类,直接将类实例化
容器对依赖注入的类\对象等进行统一接管的一个工厂
容器类-container.php
class Container
{
// 1. 对象容器
protected $instances = [];
// 2. 绑定一个类、闭包、实例、接口实现到容器
public function bind($abstract, Closure $concrete)
{
$this->instances[$abstract] = $concrete;
}
// 3. 调用容器中对象
public function make($abstract, $params=[])
{
return call_user_func_array($this->instances[$abstract], $params);
}
}
控制器-controller3.php
require 'auto_load.php';
class Controller3
{
public function fetch(Container $container)
{
$data = $container->make('model')->getData();
return $container->make('view')->fetch($data);
}
}
$container = new Container();
//绑定类到容器
$container->bind('model', function () {
return new Model();
});
$container->bind('view', function () {
return new View();
});
// 输出
echo (new Controller3())->fetch($container);
3.facade门面
Facade门面技术: 就是将服务容器中的对象的访问进行静态接管
Facade类 Facade.php
class Facade
{
//为容器中的类提供一种静态调用方式
protected static $container;
public static function initialize(Container $container)
{
static::$container = $container;
}
}
UserModel类继承facade
class UserModel extends Facade
{
public static function getData()
{
//静态绑定
return static::$container->make('model')->getData();
}
}
UserView类继承facade
class UserView extends Facade
{
public static function fetch($data)
{
//静态绑定
return static::$container->make('view')->fetch($data);
}
}
控制器controller3.php
require 'auto_load.php';
class Controller2
{
public function __construct(Container $container)
{
Facade::initialize($container);
}
public function fetch()
{
$data = UserModel::getData();
return UserView::fetch($data);
}
}
$container = new Container;
// 类绑定到容器
$container->bind('model', function () {
return new Model();
});
$container->bind('view', function () {
return new View();
});
// 输出
echo (new Controller2($container))->fetch();