学习总结
- 使用第三方包可以加快项目的开发进度
- 使用composer可以快速搭建项目框架
1.目录结构
2.composer.json文件内容
{
"autoload": {
"psr-4": {
"controllers\\": "app/controllers",
"models\\": "app/models",
"views\\": "app/views",
"core\\": "core"
}
},
"require": {
"catfan/medoo": "^1.7",
"league/plates": "^3.3",
"nikic/fast-route": "^1.3"
}
}
3.1GoodsController.php 控制器
<?php
namespace controllers;//命名空间是包名称
class GoodsController//类名必须与文件名一致
{
public function index()
{
echo $this->select();//显示/views/goods/show.php
}
public $model;
public $view;
public function __construct($model,$view)
{
$this->model = $model;
$this->view = $view;
}
public function select()
{
//使用后台medoo框架获取数据
$records = $this->model->select('tb_goods','*',['LIMIT'=>10]);
//把medoo获取的数据返回到/views/goods/show.php
return $this->view->render('goods/show', ['records' => $records]);
}
}
?>
3.2GoodsModel.php商品模型类
<?php
namespace models;
use core\Model;
//商品模型继承core中的Model
class GoodsModel extends Model
{
public function __construct()
{
parent::__construct();
}
}
?>
3.3Model.php公共模型类
<?php
namespace models;
use core\Model;
//商品模型继承core中的Model
class GoodsModel extends Model
{
public function __construct()
{
parent::__construct();
}
}
?>
3.4View.php公共视图类
<?php
namespace core;
use League\Plates\Engine;
//core中的View继承plates中的Engine
class View extends Engine
{
protected $templeate;
public function __construct($path)
{
$this->templeate = parent::__construct($path);
}
}
?>
3.5 show.php数据显示页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>商品展示页</title>
<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>
</head>
<body>
<div class="show">
<h2>商品展示页面</h2>
<div><span>编号</span><span>名称</span><span>价格</span><span>时间</span></div>
<?php foreach($records as $record):?>
<div>
<!-- 通过GoodsController控制器中的select()方法返回的数据,可直接使用 -->
<span><?= $this->e($record['id']) ?></span>
<span><?= $this->e($record['name']) ?></span>
<span><?= $this->e($record['price']) ?>\<?= $this->e($record['unit']) ?></span>
<span><?= $this->e($record['sdate']) ?></span>
</div>
<?php endforeach;?>
</div>
</body>
</html>
3.6 index.php首页
<?php
//自动加载三方控件和自已写的类,在根目录中的composer.json中的autoload字段包含自已的类
require 'vendor/autoload.php';
use controllers\GoodsController;
use core\View;
use models\GoodsModel;
//GoodsModel继承自core中的Model,core中的Model继承自medoo三方控件
$model = new GoodsModel();
//View类来自core中的View类,core中的View类继承自plates中的Engine三方控件
$view = new View('app/views');
//创建一个控件器对象,传入模型和视图对象
$controller = new GoodsController($model,$view);
//调用控制器的index方法,显示页面内容
$controller->index();
?>
3.7 页面效果展示
4 Composer命令上机效果
- 如果想自动加载自已写的类,必须在composer.json中添加autoload字段,然后用dump命令载入