LaravelKernel实例化后的处理$response=$kernel->handle($request=Illuminate\Http\Request::capture());创建并获取Request对象$request=Illuminate\Http\Request::capture()\Illuminate\Http\Requestextends\Symfony\Component\HttpFoundation\Requestpublicstaticfunctioncapture(){static::enableHttpMethodParameterOverride();returnstatic::createFromBase(SymfonyRequest::createFromGlobals());}公共静态函数enableHttpMethodParameterOverride(){self::$httpMethodParameterOverride=true;}公共静态函数createFromGlobals(){$server=$_SERVER;//CLI模式}if(array_key_exists('HTTP_CONTENT_TYPE',$_SERVER)){$server['CONTENT_TYPE']=$_SERVER['HTTP_CONTENT_TYPE'];}}//创建并返回\Symfony\Component\HttpFoundation\Request对象其实是使用全局变量来实例化对应的类(全局变量可以安全过滤),给Request对象后$request=self::createRequestFromFactory($_GET,$_POST,array(),$_COOKIE,$_FILES,$服务器);//如果标准的编码传输方式是PUT|DELETE|PATCH,从原始数据的只读流中解析数据到request属性(这个属性其实对应POST键值是的,PUT|DELETE|PATCH传输模式会转为POST模式统一处理)if(0===strpos($request->headers->get('CONTENT_TYPE'),'application/x-www-form-urlencoded')&&in_array(strtoupper($request->server->get('REQUEST_METHOD','GET')),array('PUT','DELETE','PATCH'))){parse_str($request->getContent(),$data);$request->request=newParameterBag($data);}return$request;}privatestaticfunctioncreateRequestFromFactory(数组$query=array(),数组$request=array(),数组$attributes=array(),数组$cookies=array(),数组$files=array(),array$server=array(),$content=null){//如果有自定义方法,则调用并返回if(self::$requestFactory)对应的对象{//该方法必须返回Symfony\Component\HttpFoundation\Request的对象,否则抛出异常$request=call_user_func(self::$requestFactory,$query,$request,$attributes,$cookies,$files,$server,$content);if(!$requestinstanceofself){thrownew\LogicException('请求工厂必须返回Symfony\Component\HttpFoundation\Request的一个实例。');}返回$请求;}returnnewstatic($query,$request,$attributes,$cookies,$files,$server,$content);}//创建并返回\Illuminate\Http\Request对象publicstaticfunctioncreateFromBase(SymfonyRequest$request){如果($requestinstanceofstatic){return$request;$content=$request->content;$request=(newstatic)->duplicate($request->query->all(),$request->request->all(),$request->attributes->all(),$request->cookies->all(),$request->files->all(),$request->server->all());$request->content=$content;$request->request=$request->getInputSource();返回$request;}publicfunctionduplicate(array$query=null,array$request=null,array$attributes=null,array$cookies=null,array$files=null,array$server=null){returnparent::重复($query,$request,$attributes,$cookies,$this->filterFiles($files),$server);}公共函数重复(数组$query=null,数组$request=null,数组$attributes=null,数组$cookies=null,数组$files=null,数组$server=null){$dup=clone$this;if($query!==null){$dup->query=newParameterBag($query);}if($request!==null){$dup->request=newParameterBag($request);}if($attributes!==null){$dup->attributes=newParameterBag($attributes);}if($cookies!==null){$dup->cookies=newParameterBag($cookies);}if($files!==null){$dup->files=newFileBag($files);}if($server!==null){$dup->server=newServerBag($server);$dup->headers=newHeaderBag($dup->server->getHeaders());}$dup->languages=null;$dup->charsets=null;$dup->编码=空;$dup->acceptableContentTypes=null;$dup->pathInfo=null;$dup->requestUri=null;$dup->baseUrl=null;$dup->basePath=null;$dup->方法=空;$dup->格式=空;如果(!$dup->get('_format')&&$this->get('_format')){$dup->attributes->set('_format',$this->get('_format'));}if(!$dup->getRequestFormat(null)){$dup->setRequestFormat($this->getRequestFormat(null));}return$dup;}publicfunctiongetContent($asResource=false){$currentContentIsResource=is_resource($this->content);if(PHP_VERSION_ID<50600&&false===$this->content){抛出新的\LogicException('getContent()在使用资源返回类型和PHP5.6以下时只能调用一次。');}//当资源类型时处理if(true===$asResource){if($currentContentIsResource){rewind($this->content);返回$this->content;}//传入参数(test)的内容if(is_string($this->content)){$resource=fopen('php://temp','r+');fwrite($resource,$this->content);倒带($资源);返回$资源;}$this->content=false;返回fopen('php://input','rb');}if($currentContentIsResource){rewind($this->content);返回stream_get_contents($this->content);}//否则读取标准输入字节流if(null===$this->content||false===$this->content){$this->content=file_get_contents('php://input');}return$this->content;}简而言之:最终创建了一个解析$_GET,$_POST,$_COOKIE,$IlluminateHttpRequest对象句柄处理后的_FILES、$_SERVER等变量(核心)publicfunctionhandle($request){try{$request->enableHttpMethodParameterOverride();$response=$this->sendRequestThroughRouter($request);}catch(Exception$e){$this->reportException($e);$response=$this->renderException($request,$e);}catch(Throwable$e){$this->reportException($e=newFatalThrowableError($e));$response=$this->renderException($request,$e);}事件(新事件\RequestHandled($request,$response));return$response;}//核心方法protectedfunctionsendRequestThroughRouter($request){//将请求对象注入到服务容器中供以后使用$this->app->instance('request',$request);Facade::clearResolvedInstance('request');//启动应用(包括加载和设置环境变量、加载配置文件、设置系统错误异常、Facade、启动各个服务提供者的bootstrap项等),后续分析$this->bootstrap();//委托管道处理请求,这是中间件实现后续分析return(newPipeline($this->app))的本质->发送($request)->through($this->app->shouldSkipMiddleware()?[]:$this->middleware)->then($this->dispatchToRouter());}publicstaticfunctionclearResolvedInstance($name){unset(static::$resolvedInstance[$name]);}publicfunctionbootstrap(){if(!$this->app->hasBeenBootstrapped()){$this->app->bootstrapWith($this->引导程序());}}受保护的函数bootstrappers(){######################################################################$bootstrappers=[#\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,#\Illuminate\Foundation\Bootstrap\LoadConfiguration::类,#\Illuminate\Foundation\Bootstrap\HandleExceptions::类,#\Illuminate\Foundation\Bootstrap\RegisterFacades::类,#\Illuminate\Foundation\Bootstrap\RegisterProviders::类,#\Illuminate\基金会\引导程序\BootProviders::类,#];#####################################################################返回$this->bootstrappers;}publicfunctionbootstrapWith(array$bootstrappers){$this->hasBeenBootstrapped=true;foreach($bootstrappersas$bootstrapper){//事件在启动前触发$this['events']->fire('bootstrapping:'.$bootstrapper,[$this]);//创建相应的对象并执行引导操作$this->make($bootstrapper)->bootstrap($this);//启动后事件触发$this['events']->fire('bootstrapped:'.$bootstrapper,[$this]);}}//位于Illuminate\Events\Dispatcher文件中,$payload用于给监听器传递参数,$halt表示是否终止对后续事件的监听publicfunctionfire($event,$payload=[],$halt=false){return$this->dispatch($event,$payload,$halt);}publicfunctiondispatch($event,$payload=[],$halt=false){列表($event,$payload)=$this->parseEventAndPayload($event,$payload);//如果实现了广播类,则加入广播队列if($this->shouldBroadcast($payload)){$this->broadcastEvent($payload[0]);}$响应=[];//获取与本次事件相关的监听事件函数foreach($this->getListeners($event)as$listener){$response=$listener($event,$payload);//触发事件if(!is_null($response)&&$halt){return$response;}如果($response===false){break;}$responses[]=$response;返回$halt?null:$responses;}publicfunctiongetListeners($eventName){$listeners=isset($this->listeners[$eventName])?$this->listeners[$eventName]:[];$listeners=array_merge($listeners,$this->getWildcardListeners($eventName));返回class_exists($eventName,false)?$this->addInterfaceListeners($eventName,$listeners)//在$listeners中添加接口监听事件:$listeners;}$this['events']含义参考[kernelobjectification]:1.Illuminate\Foundation\ApplicationextendsIlluminate\容器\容器2。容器实现Arr3.publicfunctionoffsetGet($key){return$this->make($key);}}4。publicfunctionoffsetSet($key,$value){$this->bind($key,$valueinstanceofClosure?$value:function()use($value){return$value;});}5.公共函数__get($key){返回$this[$key];}6.公共函数__set($key,$value){$this[$key]=$value;}7.所以$this['events']是$this->instances['events']对象($dispatcher);8。其他$this['config']类似。9.$this['events'],和$this->events一样,可以作为数组访问,也可以作为对象访问。大致流程是:创建并获取请求对象(包括请求头和请求体)=>将请求对象注入服务容器=>配置系统运行环境=>发送请求
