Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
namespace chloe\app;
use PDO;
class Model
{
public function getData()
{
return (new PDO('mysql:host=localhost;charset=utf8;dbname=phpcn', 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING]))->query('SELECT `cou_id`,`title`,`pic`,`info`,`add_time`FROM `mj_course_lists` ORDER BY `cou_id` DESC LIMIT 6')->fetchAll();
}
public function editData()
{
}
}
<?php
namespace chloe\app;
class View
{
// $data渲染到视图层的数据
public function fetch($data)
{
$table = '<table border="1" cellspacing="0" cellpadding="5" align="center">';
$table .= '<caption>课程信息表</caption>';
$table .= '
<tr align="center">
<td>编号</td>
<td>名称</td>
<td>封面</td>
<td>课程简介</td>
<td>创建时间</td>
<td>操作</td>
</tr>
';
foreach ($data as $list) {
$table .= '<tr>';
$table .= '<td>' . $list['cou_id'] . '</td>';
$table .= '<td>' . $list['title'] . '</td>';
$table .= "<td><img style='width:100px' src='{$list['pic']}' alt='课程封面' ></td>";
$table .= '<td>' . $list['info'] . '</td>';
$table .= '<td>' . date("Y-m-d H:m:s", $list['add_time']) . '</td>';
$table .= '<td><button>删除</button> <button>编辑</button> </td>';
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
}
<?php
namespace chloe\app;
// 类的自动加载
require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
// 中间层 controller 协调 m v
class Controller
{
// 将外部依赖的对象 model view 在具体的操作方法中注入进来
function index(Model $model, View $view)
{
// 获取数据
$data = $model->getData();
// 渲染数据到视图层
return $view->fetch($data);
}
function edit(Model $model, View $view)
{
$model->editData();
}
}
$model = new Model;
$view = new View;
echo (new Controller)->index($model, $view);
<?php
namespace chloe\app;
use PDO;
class Model
{
public function getData()
{
return (new PDO('mysql:host=localhost;charset=utf8;dbname=phpcn', 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING]))->query('SELECT `cou_id`,`title`,`pic`,`info`,`add_time`FROM `mj_course_lists` ORDER BY `cou_id` DESC LIMIT 6')->fetchAll();
}
public function editData()
{
}
}
view.php代码为
<?php
namespace chloe\app;
class View
{
// $data渲染到视图层的数据
public function fetch($data)
{
$table = '<table border="1" cellspacing="0" cellpadding="5" align="center">';
$table .= '<caption>课程信息表</caption>';
$table .= '
<tr align="center">
<td>编号</td>
<td>名称</td>
<td>封面</td>
<td>课程简介</td>
<td>创建时间</td>
<td>操作</td>
</tr>
';
foreach ($data as $list) {
$table .= '<tr>';
$table .= '<td>' . $list['cou_id'] . '</td>';
$table .= '<td>' . $list['title'] . '</td>';
$table .= "<td><img style='width:100px' src='{$list['pic']}' alt='课程封面' ></td>";
$table .= '<td>' . $list['info'] . '</td>';
$table .= '<td>' . date("Y-m-d H:m:s", $list['add_time']) . '</td>';
$table .= '<td><button>删除</button> <button>编辑</button> </td>';
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
}
controller.php代码为
<?php
namespace chloe\app;
// 类的自动加载
require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
class Controller
{
// 将外部依赖的对象 model view 在具体的操作方法中注入进来
function index(Model $model, View $view)
{
// 获取数据
$data = $model->getData();
// 渲染数据到视图层
return $view->fetch($data);
}
function edit(Model $model, View $view)
{
$model->editData();
}
}
$model = new Model;
$view = new View;
echo (new Controller)->index($model, $view);
<?php
namespace chloe\app;
use Closure;
// 服务容器 一个自动生产类 /对象的工厂
class Container
{
// 对象容器
protected $instances = [];
/**
* 绑定一个类标识、闭包、实例到容器
* @access public
* @param string|array $abstract 类标识或者接口的别名 alias
* @param mixed $concrete 要绑定的类、闭包或者实例
*
*/
// 往容器数组中绑定对象
public function bind($abstract, Closure $concrete)
{
$this->instances[$abstract] = $concrete;
}
public function make($abstract, $params = [])
{
return call_user_func_array($this->instances[$abstract], $params);
}
}
$container = new Container;
// 绑定一个闭包到服务容器中 我们使用$model对象时才去实例化
$container->bind('model', function () {
return new Model;
});
$container->bind('view', function () {
return new View;
});
$container->make('model')->getData();