# 上下文-Context
[TOC]
middleware,controller,model,task,在一個消息流程中共享同一個上下文Context。
其中middleware,controller,model可以修改Context,而task只能讀取不能修改。
例如MonitorMiddleware
```
public function after_handle($path)
{
$this->context['path'] = $path;
$this->context['execution_time'] = (microtime(true) - $this->start_run_time) * 1000;
if (self::$efficiency_monitor_enable) {
$this->log('Monitor');
}
}
```
這里將context附加了path和execution_time,在after_handle流程前所有對context的操作都會影響到這里的context。
## Controller,Model,Task
通過getContext獲取當前的上下文。
## RunStack
Context中包含RunStack字段,這個字段將記錄消息流程中所執行的Middleware,Controller,Model,Task的方法。
例如:
```
Array
(
[RunStack] => Array
(
[0] => Server\Middlewares\MonitorMiddleware::before_handle
[1] => Server\Middlewares\NormalHttpMiddleware::before_handle
[2] => Server\Controllers\TestController::setRequestResponse
[3] => Server\Controllers\TestController::http_testContext
[4] => Server\Models\TestModel::contextTest
[5] => Server\Tasks\TestTask::contextTest
[6] => Server\CoreBase\TaskProxy::startTask
[7] => Server\Models\TestModel::destroy
[8] => Server\Controllers\TestController::destroy
[9] => Server\Middlewares\NormalHttpMiddleware::after_handle
[10] => Server\Middlewares\MonitorMiddleware::after_handle
)
[request_id] => 15081258371921825039
[controller_name] => TestController
[method_name] => TestController:http_testContext
[test] => 1
[path] => /TestController/testContext
[execution_time] => 4.1890144348145
)
```
通過這個可以精確判斷發生異常和錯誤的位置,也可以了解到SD框架的工作流程。
- SD3.X簡介
- 捐贈SD項目
- VIP服務
- 基礎篇
- 搭建環境
- 使用Composer安裝/更新SD框架
- 啟動命令
- 開發注意事項
- 框架配置
- 配置文件夾
- server.php
- ports.php
- business.php
- mysql.php
- redis.php
- timerTask.php
- log.php
- consul.php
- catCache.php
- client.php
- 自定義配置
- 框架入口
- MVC架構
- 加載器-Loader
- 控制器-Controller
- 模型-Model
- 視圖-View
- 同步任務-Task
- 封裝器
- Swoole編程指南-EOF協議
- Swoole編程指南-固定包頭協議
- 封裝器-Pack
- 路由器
- TCP相關
- 綁定UID
- Send系列
- Sub/Pub
- 獲取服務器信息
- Http相關
- HttpInput
- HttpOutput
- 默認路由規則
- WebSocket相關
- 使用SSL
- 公共函數
- 進階篇
- 內核優化
- 封裝器路由器原理剖析
- 對象池
- 上下文-Context
- 中間件
- 進程管理
- 創建自定義進程
- 進程間RPC
- 自定義進程如何使用連接池
- 異步連接池
- Redis
- Mysql
- Mqtt
- HttpClient
- Client
- AMQP
- RPC
- 日志工具-GrayLog
- 微服務-Consul
- Consul基礎
- 搭建Consul服務器
- SD中Consul配置
- 微服務
- 選舉-Leader
- Consul動態配置定時任務
- 熔斷與降級
- 集群-Cluster
- 高速緩存-CatCache
- 萬物-Actor
- Actor原型
- Actor的創建
- Actor間的通訊
- 消息派發-EventDispatcher
- 延遲隊列-TimerCallBack
- 協程
- 訂閱與發布
- MQTT簡易服務器
- AMQP異步任務調度
- 自定義命令-Console
- 調試工具Channel
- 特別注意事項
- 日常問題總結
- 實踐案例
- 物聯網自定義協議
- Actor在游戲的應用
- Mongodb以及一些同步擴展的使用
- 自定義進程使用MQTT客戶端
- 開發者工具
- SDHelper