PHP闭包的路由实例模型
来源:互联网 发布:latex for windows 编辑:程序博客网 时间:2024/05/16 14:05
<?php/* 回调函数 */class App { protected $routes = []; protected $responseStatus = '200 OK'; protected $responseContentType = 'text/html'; protected $responseBody = 'Hello World'; public function addRoute($routePath, $routeCallback) { // 将路由的回调绑定到当前的应用对象上 $this->routes[$routePath] = $routeCallback->bindTo($this, __CLASS__); } public function dispatch($currentPath) { foreach ($this->routes as $routePath => $callback) { if ($currentPath == $routePath) { $callback(); } } header('HTTP/1.1' . $this->responseStatus); header('Content-type: '.$this->responseContentType); header('Content-length: '.mb_strlen($this->responseBody)); echo $this->responseBody; }}$app = new App();$app->addRoute('/users/josh', function() { // 在回调函数的上下文环境已经切换到app的实例 $this->responseContentType = 'application/json;charset=utf8'; $this->responseBody = '{"name": "yanming"}';});$app->dispatch('/users/josh');
阅读全文
0 0
- PHP闭包的路由实例模型
- php url路由入门实例
- PHP的路由浅析
- php的路由规则
- PHP的路由浅析
- PHP的闭包
- PHP的闭包
- PHP的闭包
- PHP的闭包
- 闭包相关的实例
- PHP的配置路由控制器
- PHP 闭包函数 function use 用法实例
- php闭包的作用
- php的闭包写法
- 谈消息总线的路由模型
- 本机发出的包的路由
- javascript闭包的理解和实例
- Swift - 函数 & 闭包的应用实例
- C语言之推箱子游戏代码
- 浅谈php中的回调函数和匿名函数
- 6月14日 了解unittest、输出测试报告
- 实际开发中-Block导致循环引用的问题
- Hive元数据库介绍及信息查看
- PHP闭包的路由实例模型
- spark搭建和java示例
- Fiddler的详细介绍
- iptables介绍
- numpy学习——数组性能比较
- 车架号vin码识别OCR识别原理
- 概念区分:JavaScript中的global对象,window对象以及document对象
- github入门
- 面试题3:二维数组的查找