批改状态:合格
老师批语:这个案例将我们之前的不少函数知识都用到了

<?phpnamespace 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;}}}?>
<?phpnamespace 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>编号</span><span>名称</span><span>价格</span><span>时间</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>';}}}?>
<?phpnamespace 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]);}}}?>
<?phpnamespace 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;}}}?>
<?phpnamespace 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 门面技术<br>';//创建一个服务容器的对象$container = new Container();//服务容器绑定数据$container->bind('model',function(){ return new Model; });$container->bind('view',function(){ return new View; });//创建一个控制器$controller4 = new Controller4($container);echo $controller4->index();//对象销毁$container->delete('model');$container->delete('view');}?>
<?phpclass GoodsController{public function showdata($id,$name){echo '商品编号:',$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'];//过虑空数组元素,生成一个数组$pathInfoArr = array_values(array_filter(explode('/',$pathInfoStr)));//获取要访问的控制器$controller =ucfirst(array_shift($pathInfoArr)) .'Controller';//获取要访问的控制器的方法$method = array_shift($pathInfoArr);//获取参数$paras = [];for($i=0;$i<count($pathInfoArr);$i+=2)://如果参数没有值就不解析这个参数if(isset($pathInfoArr[$i+1])):$paras[$pathInfoArr[$i]] = $pathInfoArr[$i+1];endif;endfor;//动态生成一个控制器类,动态调用控制器类中的方法$goods = new $controller();$goods->$method(...array_values($paras));?>

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号