
批改狀態(tài):合格
老師批語:這個(gè)案例將我們之前的不少函數(shù)知識(shí)都用到了
<?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;
}
}
}
?>
<?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>編號(hào)</span><span>名稱</span><span>價(jià)格</span><span>時(shí)間</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>';
}
}
}
?>
<?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]);
}
}
}
?>
<?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;
}
}
}
?>
<?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 門面技術(shù)<br>';
//創(chuàng)建一個(gè)服務(wù)容器的對(duì)象
$container = new Container();
//服務(wù)容器綁定數(shù)據(jù)
$container->bind('model',function(){ return new Model; });
$container->bind('view',function(){ return new View; });
//創(chuàng)建一個(gè)控制器
$controller4 = new Controller4($container);
echo $controller4->index();
//對(duì)象銷毀
$container->delete('model');
$container->delete('view');
}
?>
<?php
class GoodsController
{
public function showdata($id,$name)
{
echo '商品編號(hào):',$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'];
//過慮空數(shù)組元素,生成一個(gè)數(shù)組
$pathInfoArr = array_values(array_filter(explode('/',$pathInfoStr)));
//獲取要訪問的控制器
$controller =ucfirst(array_shift($pathInfoArr)) .'Controller';
//獲取要訪問的控制器的方法
$method = array_shift($pathInfoArr);
//獲取參數(shù)
$paras = [];
for($i=0;$i<count($pathInfoArr);$i+=2):
//如果參數(shù)沒有值就不解析這個(gè)參數(shù)
if(isset($pathInfoArr[$i+1])):
$paras[$pathInfoArr[$i]] = $pathInfoArr[$i+1];
endif;
endfor;
//動(dòng)態(tài)生成一個(gè)控制器類,動(dòng)態(tài)調(diào)用控制器類中的方法
$goods = new $controller();
$goods->$method(...array_values($paras));
?>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)