ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
# 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(); } ```