Correction status:qualified
Teacher's comments:代码出来了, 关键是要理解并记住
路由的原理与实现
1.从pathinfo分割出独立单元保存为数组;
2.从pathinfo解析出模块/控制器/操作;
3.从pathinfo解析出变量键值对;
<?php //print_r($_SERVER['REQUEST_URI']); //演示地址:http://php.io/%E4%BD%9C%E4%B8%9A/0807/index.php/hello/id/1/name/hjc //获取URL地址 $url = $_SERVER['REQUEST_URI']; //分割成独立单元保存为数组 $request = explode('/',$url); //echo '<pre>'.print_r($request,true); //将键名action绑定变量方法名 $pathinfo['action'] = $request['4']; //echo '<pre>'.print_r($pathinfo,true); //offset偏移量,将偏移量之后的键值对另存为一个数组 $values = array_slice($request,5); //echo '<pre>'.print_r($values,true); //路由一个函数,闭包函数,use() 连接闭包和外界变量。 $pathinfo['action'] = function (...$values) use ($pathinfo) { // echo '<pre>'.print_r($values,true); for ($i=0;$i<count($values);$i=$i+2) { if (isset($values[$i+1])) { $params[$values[$i]] = $values[$i+1]; } } $res = ''; foreach ($params as $key => $value) { $res .= $key . '='.$value.','; } return '方法名:'.$pathinfo['action'].'键值对:'.$res; }; echo call_user_func_array($pathinfo['action'],$values);
点击 "运行实例" 按钮查看在线实例
关于本节课其他笔记:
容器的原理:
是一个类,需至少提供2个功能,一个创建类实例,一个取出类实例;
步骤:
1.初始化一个空数组 protected $instance = [];
2.将某个类实例绑定在容器中 $this->instance[$alias]=$process
3.从容器中取出对象 return call_user_func_array($this->instance[$alias],$params);
4.将模型与视图的实例绑定到容器
5.创建控制器,实例化并打印出来;
<?php namespace demo1; require 'Model1.php'; require 'View1.php'; class Container { // 类实力数组,对象池 protected $instance = []; // 将某个类绑定到容器中 public function bind($alias, $process) { $this->instance[$alias] = $process; } // 从容器中取出对象 public function make($alias,$params = []) { return call_user_func_array($this->instance[$alias],$params); } } $container = new Container(); $container->bind('Model1',function (){return new \Model1();}); $container->bind('View1',function (){return new \View1();}); //控制器 class Controller { public function index(Container $container) { $data = $container->make('Model1')->getData(); return $container->make('View1')->fetch($data); } } $controller = new Controller(); echo $controller->index($container);
点击 "运行实例" 按钮查看在线实例
门面原理:
1.创建容器类实例和模型数据
2.初始化方法对$container进行后期静态绑定
3.创建获取数据静态接口和渲染模板的静态接口
4.创建商品继承门面进行实例化
<?php require 'Model1.php'; require 'View1.php'; class Container { //对象池 protected $instance = []; //实例绑定到容器中 public function bind ($alias , Closure $process) { //类实力保存到容器中 $this->instance[$alias] = $process; } public function make($alias,$params=[]) { return call_user_func_array($this->instance[$alias],$params); } } //将模型与视图的实例绑定到容器中 $container = new Container(); $container->bind('Model1',function (){return new Model1();}); $container->bind('View1',function (){return new View1();}); //门面 class Facade { //容器类的实例 protected static $container = null; //模型数据 protected static $data = []; //初始化方法 public static function initialize(Container $container) { static::$container = $container; //后期静态绑定 } //获取模型数据静态接口 public static function getData() { static::$data = static::$container->make('Model1')->getData(); } //渲染模板的静态接口 public static function fetch() { return static::$container->make('View1')->fetch(static::$data); } } //商品类 class Product extends Facade { } //客户端控制器 class Controller { public function __construct(Container $container) { Product::initialize($container); } public function index() { Product::getData(); return Product::fetch(); } } $controller = new Controller($container); echo $controller->index();
点击 "运行实例" 按钮查看在线实例