MVC框架
先上图
客户端与控制器部分
控制器:controller.php
<?php
//服务容器层
namespace controller;
use Closure;
use config\model;
use view\header;
use view\index;
use view\listinfo;
use view\article;
use view\footer;
// require __DIR__ . '/atuoload.php';
require __DIR__ . '/config/model.php';
require __DIR__ . '/view/index.php';
require __DIR__ . '/view/header.php';
require __DIR__ . '/view/listinfo.php';
require __DIR__ . '/view/article.php';
require __DIR__ . '/view/footer.php';
// 服务容器类
class container{
//容器属性时一个空数组,里面装创建对象的方法
protected $instance = [];
// 放进去,把实例化过程绑定到容器中
// $alias 类实类的别名 可以自定义;Closure用于代表 匿名函数 的类
public function bind($alias, Closure $process)
{
//将类实例化的方法 存储到容器中
$this->instance[$alias] = $process;
}
//取出来,执行容器中的实例方法
public function make($alias, $arr = [])
{
//call_user_func_array 回调函数,第一个是回调函数,第二个时索引数组
//如call_user_func_array($a, $b) 把($a)作为回调函数调用,把参数数组作($b)为回调函数的参数传入。
return call_user_func_array($this->instance[$alias], $arr);
}
}
$container = new container();
$container->bind( 'model', function(){return new model();} );
$container->bind('header', function(){return new header();});
$container->bind('index', function(){return new index();});
$container->bind('listinfo', function(){return new listinfo();});
$container->bind('article', function(){return new article();});
$container->bind('footer', function(){return new footer();});
// facade门面类
class Facade{
private static $container = [];
private static $data = [];
// 导入容器,并静态化
public static function initialize(container $container){
static::$container = $container;
}
// 导出model,并静态化
public static function model(){
static::$data = static::$container->make('model')->getData();
}
// 导出视图index层,并静态化
public static function index(){
return static::$container->make('index')->index(static::$data);
}
// 导出视图header层,并静态化
public static function header(){
return static::$container->make('header')->head(static::$data);
}
// 导出视图list层,并静态化
public static function listinfo(){
return static::$container->make('listinfo')->listinfo(static::$data);
}
// 导出视图article层,并静态化
public static function article(){
return static::$container->make('article')->article(static::$data);
}
// 导出视图footer层,并静态化
public static function footer(){
return static::$container->make('footer')->footer();
}
}
//控制器
class demo{
public function __construct(container $container)
{
Facade::initialize($container);
}
public function index(){
//导入数据
Facade::model();
//渲染首页模板
Facade::header();
Facade::index();
Facade::footer();
}
public function list(){
//导入数据
Facade::model();
//渲染列表页模板
Facade::header();
Facade::listinfo();
Facade::footer();
}
public function article(){
//导入数据
Facade::model();
//渲染内容页模板
Facade::header();
Facade::article();
Facade::footer();
}
}
$demo = new demo($container);
客户端首页:index.php
<?php
require __DIR__ . '/controller.php';
//客户端 首页
echo $demo->index();
客户端列表页:list.php
注意:在模板中使用了 list.php?id= 请注意命名
<?php
require __DIR__ . '/controller.php';
//客户端 列表页
echo $demo->list();
客户端 内容页:article.php
注意:在模板中使用了 article.php?id= 请注意命名
<?php
require __DIR__ . '/controller.php';
//客户端 内容页
echo $demo->article();
数据库model.php
<?php
namespace config;
use PDO;
class model{
//pdo
private static $pdo;
//数据库表
private static $table = null;
public function __construct($dns='mysql:host=localhost;dbname=film',$user='root',$password='root')
{
static::$pdo = new PDO($dns,$user,$password);
}
//获取电影栏目表
private static function listinfo($table='listinfo'){
static::$table = $table;
$sql = 'SELECT * FROM ' . static::$table;
$stmt = static::$pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
//获取电影内容表
private static function article($table='article'){
static::$table = $table;
$sql = 'SELECT * FROM ' . static::$table;
$stmt = static::$pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
//获取会员表
private static function user($table='user'){
static::$table = $table;
$sql = 'SELECT * FROM ' . static::$table . ' LIMIT 0,3';
$stmt = static::$pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
//将电影栏目和内容详情页,会员数据包整合到一个类中
public function getData(){
return [
'article'=>static::article(),
'list'=>static::listinfo(),
'user'=>static::user()
];
}
}
1. 注意栏目主键与内容页索引关联
视图模板层
1.$vv[‘uid’] == $v[‘id’] 才能对应调用
index.php
<?php
namespace view;
class index{
public function index($data){
foreach ($data['list'] as $v){
echo '<h2><a href="list.php?id=' . $v['id'] . '">' . $v['title'] . '</a></h2>';
echo '<ul>';
foreach($data['article'] as $vv){
if($vv['uid'] == $v['id']){
echo '<li><a href="article.php?id='. $vv['id'] .'">'.$vv['title'].'</a></li>';
}
}
echo '</ul>';
}
}
}
?>
header.php
<?php
namespace view;
class header{
public function head($data)
{
echo '<header>';
echo '<nav>';
echo '<a href="/">首页</a>';
foreach($data['list'] as $v){
echo "<a href=list.php?id={$v['id']}>{$v['title']}</a>";
}
echo '</nav>';
echo '</header>';
}
}
echo '
<style>
a{text-decoration: none;color:#333;}
header{width: 100%; height: 60px;background-color: lightcoral;}
header>nav{height: 100%;display:grid;grid-template-columns: repeat(4,1fr);place-items: center;}
header>nav>a{color:white;text-decoration: none;font-size: 20px;}
footer{width: 100%; height: 60px;background-color: lightcoral;display: flex;justify-content: center;align-items: center;color: white;}
</style>';
?>
listinfo.php
通过$_GET对应调用
<?php
namespace view;
class listinfo{
public function listinfo($data){
foreach ($data['list'] as $v) {
if($_GET['id'] == $v['id']){
echo '<h2><a href="list.php?id=' . $_GET['id'] . '">' . $v['title'] . '</a></h2>';
}
}
echo '<ul>';
foreach ($data['article'] as $v) {
if($_GET['id'] == $v['uid']){
echo '<li><a href=article.php?id='.$v['id'].'>' . $v['title'] . '</a></li>';
}
}
echo '</ul>';
}
}
?>
article.php
<?php
namespace view;
class article{
public function article($data){
foreach ($data['article'] as $v) {
if($_GET['id'] == $v['id']){
echo '<h2>' . $v['title'] . '</h2>';
echo '<img src=' . $v['img'] . '>';
echo '<p>' . $v['newstext'] . '<p>';
}
}
}
}
?>
footer.php
<?php
namespace view;
class footer{
public function footer(){
echo '<footer><p>php中文网</p></footer>';
}
}
总结:
1、完成了一个傻瓜式mvc,真的傻瓜式。
2、php相关知识串联学习,收获颇丰
3、来不及做会员登陆了,但已经有思路了