当前位置: 首页 > 后端技术 > PHP

lumen5.5学习(三)

时间:2023-03-29 16:13:02 PHP

接上一篇$app->run();------------------分割线----------------------$app是一个Application的实例,但是在Application.php文件中找不到run方法。在类里面,可以看到使用ConcernsRoutesRequests,打开它,找到run方法。run方法注释主要用于运行应用程序并发送响应/***运行应用程序并发送响应。**@paramSymfonyRequest|null$request*@returnvoid*/publicfunctionrun($request=null){$response=$this->dispatch($request);if($responseinstanceofSymfonyResponse){$response->send();}else{echo(string)$response;}if(count($this->middleware)>0){$this->callTerminableMiddleware($response);}}主要看dispatch方法/***dispatch传入的请求。**@paramSymfonyRequest|null$request*@returnResponse*/publicfunctiondispatch($request=null){list($method,$pathInfo)=$this->parseIncomingRequest($request);尝试{返回$this->sendThroughPipeline($this->middleware,function()use($method,$pathInfo){if(isset($this->router->getRoutes()[$method.$pathInfo])){返回$this->handleFoundRoute([true,$this->router->getRoutes()[$method.$pathInfo]['action'],[]]);}返回$this->handleDispatcherResponse($this->createDispatcher()->dispatch($method,$pathInfo));});}catch(Exception$e){return$this->prepareResponse($this->sendExceptionToHandler($e));}catch(Throwable$e){return$this->prepareResponse($this->sendExceptionToHandler($e));}}因为请求的URL是api.com/index.php/$method.$pathInfo,所以打印输出是array(1){[0]=>string(4)"GET/"}$this->router->getRoutes()是获取web.php中定义的所有路由,返回一个数据数组:array(size=3)'GET/'=>array(size=3)'method'=>string'GET'(length=3)'uri'=>string'/'(length=1)'action'=>array(size=1)0=>object(Closure)[10]public'static'=>array(size=1)'router'=>object(Laravel\Lumen\Routing\Router)[6]public'app'=>......所以isset($this->router->getRoutes()[$method.$pathInfo])的结果就是true调用handleFoundRoute方法/***处理调度器找到的路由。**@param数组$routeInfo*@returnmixed*/protectedfunctionhandleFoundRoute($routeInfo){$this->currentRoute=$routeInfo;$this['request']->setRouteResolver(function(){return$this->currentRoute;});$action=$routeInfo[1];//通过路由中间件进行管道...if(isset($action['middleware'])){$middleware=$this->gatherMiddlewareClassNames($action['middleware']);返回$this->prepareResponse($this->sendThroughPipeline($middleware,function(){return$this->callActionOnArrayBasedRoute($this['request']->route());}));}返回$this->prepareResponse($this->callActionOnArrayBasedRoute($routeInfo));}如果有中间件配置,就会有中间件处理(后面会写中间件在web.php中匹配这条路由后,可以看到这段代码$this->callActionOnArrayBasedRoute($routeInfo)/***调用Closure在基于数组的路由上。**@paramarray$routeInfo*@returnmixed*/protectedfunctioncallActionOnArrayBasedRoute($routeInfo){$action=$routeInfo[1];if(isset($action['uses'])){返回$this->prepareResponse($this->callControllerAction($routeInfo));}foreach($actionas$value){if($valueinstanceofClosure){$closure=$value->bindTo(newRoutingClosure);休息;}}try{return$this->prepareResponse($this->call($closure,$routeInfo[2]));}catch(HttpResponseException$e){返回$e->getResponse();}}是处理路由对应的response方法,组装响应数据准备返回定义的路由是$router->get('/',function()use($router){return$router->应用->版本();});匹配路由后,对应的处理是function()use($router){return$router->app->version();//'Lumen(5.5.2)(LaravelComponents5.5.*)'}所以$this->call($closure,$routeInfo[2])得到'Lumen(5.5.2)(LaravelComponents5.5.*)'然后将处理后的数据传入$this->prepareResponse()方法中,组装响应请求数据。组装好数据后,回到原来的run方法然后往下$response->send()追踪到SymfonyComponentHttpFoundationResponse类(组装响应数据也在这里)/***发送当前web响应的内容.**@return$this*/publicfunctionsendContent(){echo$this->content;返回$th是;}/***发送HTTP标头和内容。**@return$this*/publicfunctionsend(){$this->sendHeaders();$this->sendContent();如果(function_exists('fastcgi_finish_request')){fastcgi_finish_request();}elseif('cli'!==PHP_SAPI){static::closeOutputBuffers(0,true);}返回$this;从这里可以看出这里呼应了'Lumen(5.5.2)(LaravelComponents5.5.*)'至此,可以大致知道框架从请求到响应的基本流程。还有一些中间件,事件监听等知识后面会补充。有什么不对的地方记得指出来,互相学习~