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

Laravel配置项即时加载服务提供者

时间:2023-03-29 20:46:47 PHP

Laravel配置项即时加载服务提供者(根目录:/var/www/laravel/)所有即时加载服务注册完成后,会立即调用register方法,并将其标记为已加载。然后通过\Illuminate\Foundation\Bootstrap\BootProviders启动项调用所有即时加载服务提供者的boot方法勾选bootstrap/cache/services.php'eager'=>array(//Systemserviceprovider0=>'Illuminate\\Auth\\AuthServiceProvider',//注入认证相关对象1=>'Illuminate\\Cookie\\CookieServiceProvider',//注入cookie对象2=>'Illuminate\\Database\\DatabaseServiceProvider',//注入db相关objects3=>'Illuminate\\Encryption\\EncryptionServiceProvider',//注入加解密对象4=>'Illuminate\\Filesystem\\FilesystemServiceProvider',//注入文件相关对象5=>'Illuminate\\Foundation\\Providers\\FoundationServiceProvider',//注入基本请求对象6=>'Illuminate\\Notifications\\NotificationServiceProvider',//注入通知对象7=>'Illuminate\\Pagination\\PaginationServiceProvider',//注入分页-relatedobjects8=>'Illuminate\\Session\\SessionServiceProvider',//注入session相关的对象9=>'Illuminate\\View\\ViewServiceProvider',//注入视图相关对象10=>'Laravel\\Passport\\PassportServiceProvider',//注入护照相关对象//配置项服务提供者11=>'App\\Providers\\AppServiceProvider',12=>'App\\Providers\\AuthServiceProvider',13=>'App\\Providers\\EventServiceProvider',14=>'App\\Providers\\RouteServiceProvider',)路由相关Serviceprovider//主要是调用App\\Providers\\RouteServiceProviderpublicfunctionboot(){//可以添加自己的操作parent::boot();}publicfunctionboot(){$this->setRootControllerNamespace();//如果执行phpartisanroutes:cache(自动生成路由缓存文件,注意:建议只在项目在线时操作),直接加载if($this->app->routesAreCached()){$this->loadCachedRoutes();}else{//加载路由$this->loadRoutes();//设置系统启动时的事件监听函数$this->app->booted(function(){$this->app['router']->getRoutes()->refreshNameLookups();});}}protectedfunctionsetRootControllerNamespace(){if(!is_null($this->namespace)){//设置$this->instances['url'](\Illuminate\Routing\UrlGenerator对象)的rootNamespace属性$this->app[UrlGenerator::class]->setRootControllerNamespace($this->namespace);}}publicfunctionroutesAreCached(){返回$this['files']->exists($this->getCachedRoutesPath());}publicfunctiongetCachedRoutesPath(){return$this->bootstrapPath().'/cache/routes.php';}protectedfunctionloadRoutes(){if(method_exists($this,'map')){$this->app->call([$this,'map']);}}publicfunctionmap(){$this->mapApiRoutes();$this->mapWebRoutes();}//API相关的路由受保护的函数mapApiRoutes(){Route::prefix('api')->middleware('api')->namespace($this->namespace)->group(base_path('routes/api.php'));}//WEB相关的路由protectedfunctionmapWebRoutes(){Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php'));}publicfunctionbooted($callback){$this->bootedCallbacks[]=$打回来;//如果应用已经启动,直接调用if($this->isBooted()){$this->fireAppCallbacks([$callback]);}}//Route是对Facde调用方法的解析:Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php'));Route::middlewarepublicstaticfunction__callStatic($method,$args){//获取应用的路由器对象$instance=static::getFacadeRoot();if(!$instance){thrownewRuntimeException('Afacaderoothasnotbeenset.');}//将Router::middleware()转换为应用的Router->middleware()方法,如果Router没有中间件方法,会直接触发__callreturn$instance->$method(...$args);}公共函数__call($method,$parameters){if(static::hasMacro($method)){return$this->macroCall($method,$parameters);}//将不存在方法的调试委托给RouteRegistrar处理,return(newRouteRegistrar($this))->attribute($method,$parameters[0]);}publicfunctionattribute($key,$value){//只允许指定的属性设置($allowedAttributes=['as','domain','middleware','name','namespace','prefix',])if(!in_array($key,$this->allowedAttributes)){thrownewInvalidArgumentException("属性[{$key}]不存在。");}//支持.方法来存储属性$this->attributes[array_get($this->aliases,$key,$key)]=$value;返回$this;}公共函数组($callback){$this->router->group($this->attributes,$callback);}publicfunctiongroup(array$attributes,$routes){$this->updateGroupStack($属性);$this->loadRoutes($routes);array_pop($this->groupStack);}保护函数loadRoutes($routes){if($routesinstanceofClosure){$routes($this);}else{$router=$this;需要$路由;}}小结Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php'));其实就是在RouteRegistrar对象中的attributes数组属性中添加中间件命名空间,返回RouteRegistrar对象,然后在RouteRegistrar对象中调用路由器对象的group方法,也就是调用路由器的group方法对象,最后将['middleware'=>'web','namespace'=>$this->namespace]数组设置为$this->router的groupStack属性(将用于所有路由),然后加载config/web.php。