# Context
上下文共享
middleware,controller,model,task,在一個消息流程中共享同一個上下文Context。
其中middleware,controller,model可以修改Context,而task只能讀取不能修改。
例如MonitorMiddleware
```php
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框架的工作流程。
你可以在AppServer的__construct方法中設置$this->setDebugMode()來開啟這個打印。
```
public function __construct()
{
$this->setLoader(new Loader());
$this->setDebugMode();
parent::__construct();
}
```
- Introduction
- SD 3.X文檔連接
- 導言
- 用戶案例
- 基于Swoole擴展分布式全棧開發框架
- 選擇SD框架助力企業開發
- 捐贈SwooleDistributed項目
- 框架性能報告
- 更新日志
- VIP服務福利
- 安裝與配置
- 【推薦】全自動安裝部署
- 環境要求
- 使用Composer安裝/更新SD框架
- 通過Docker安裝
- 代碼結構
- 啟動命令
- 服務器配置
- 服務器基礎配置server.php
- 客戶端協議配置client.php
- business.php
- log.php
- 微服務及集群配置consul.php
- fileHeader.php
- mysql.php
- redis.php
- 定時任務配置timerTask.php
- 服務器端口配置ports.php
- catCache.php
- 驗證服務啟動成功
- 微服務-Consul
- 日志工具-GrayLog
- 集群-Cluster
- 內核優化
- 入門教學
- 開發流程
- 開發前必讀
- 開發規范
- 基本流程
- 框架入口
- Model數據模型
- Controller控制器
- 協程
- 協程基礎
- 迭代器
- 調度器
- 使用協程的優勢
- 通過協程的方法屏蔽異步同步的區別
- Select多路選擇器
- 協程Sleep
- 通用協程方法
- 設置超時
- 設置無異常
- 設置降級函數
- initAsynPools
- dump
- 封裝器與路由器
- 封裝器
- sendToUid
- 路由器
- sendToUids
- 對象池
- 擴展組件
- 中間件
- Redis使用介紹
- RedisAsynPool
- Redis具體使用
- sendToAll
- RedisRoute
- Redis+Lua
- Mysql使用介紹
- MysqlAsynPool
- Mysql返回值
- 如何獲取構建的mysql語句
- 如何執行一個SQL
- 如何執行事務
- stopTask
- Mysql具體使用
- 異步客戶端
- Loader
- MqttClient
- model
- SdTcpRpcPool
- task
- HttpClientPool
- view
- TcpClientPool
- AMQP
- initialization
- Memory
- destory
- Cache
- Lock
- Pool
- EventDispatcher
- Process
- Cluster
- TimerTask
- Reload
- Consul
- Context
- 自定義進程
- 進程間RPC
- $http_input
- CatCache
- $http_output
- TimerCallBack
- 專題
- HTTP專欄
- TCP專欄
- 基礎知識
- WebSocket專欄
- 微服務
- Consul配置
- RPC
- REST
- AMQP異步任務系統
- MQTT簡易服務器
- Docker化以及資源編排
- 快速搭建公司內部統一的開發環境
- 使用HTTPS/WSS
- 訂閱/發布
- 游戲專題
- 類介紹
- AppServer
- clearState
- onOpenServiceInitialization
- SwooleDistributedServer
- get_instance
- kickUid
- bindUid
- unBindUid
- coroutineUidIsOnline
- coroutineCountOnline
- setTemplateEngine
- isWebSocket
- isTaskWorker
- getSocketName
- initAsynPools
- addAsynPool
- getAsynPool
- getServerAllTaskMessage
- Controller
- onExceptionHandle
- send
- sendToUid
- sendToUids
- sendToAll
- sendToGroup
- close
- getContext
- defaultMethod
- $redis_pool
- $mysql_pool
- $request_type
- $fd
- $uid
- $client_data
- $request
- $response
- $loader
- $logger
- $server
- $config
- Model
- initialization
- destory
- View
- Task
- stopTask
- HttpInput
- postGet
- post
- get
- getPost
- getAllPostGet
- getAllHeader
- getRawContent
- cookie
- getRequestHeader
- server信息
- getRequestMethod
- getRequestUri
- getPathInfo
- HttpOutput
- setStatusHeader
- setContentType
- setHeader
- end
- setCookie
- endFile
- 單元測試