本文来自SF社区提问。为什么可以直接使用laravelweb.php中的Route?原理很简单1.首先注意/config/app.php中的/*--------------------------------------------------------------|类别名|----------------------------------------------------------------------||这个类别名数组将在这个应用程序|时被注册。开始了。但是,请随意注册,数量不限|别名是“惰性”加载的,因此它们不会影响性能。|*/'aliases'=>['Route'=>Illuminate\Support\Facades\Route::class,];2.因为有Facades,我们直接看Illuminate\Support\Facades\Route::class*这个类返回的内容@methodstatic\Illuminate\Routing\Routeget(string$uri,\Closure|array|string$action)/***获取组件的注册名。**@returnstring*/protectedstaticfunctiongetFacadeAccessor(){return'router';}3.很简单,直接进入注册为router的组件,发现在Illuminate/Routing/RoutingServiceProvider.php/***注册路由器实例。**@returnvoid*/protectedfunctionregisterRouter(){$this->app->singleton('router',function($app){returnnewRouter($app['events'],$app);});}4。newRouter()如果你看到它,它显然会返回Illuminate/Routing/Router.php实例;你找到了吗/***向路由器注册一个新的GET路由。**@paramstring$uri*@param\Closure|array|string|null$action*@return\Illuminate\Routing\Route*/publicfunctionget($uri,$action=null){return$this->addRoute复制代码(['GET','HEAD'],$uri,$action);}(づ ̄3 ̄)づ╭?~通知我!!问答时间1).我确认“router”在Illuminate/Routing/RoutingServiceProvider.php中,但为什么它没有在/config/app.php的提供程序中配置?答案在这里Illuminate/Foundation/Application.php注意基础服务提供者和配置的提供者/***注册所有的基础服务提供者。**@returnvoid*/保护函数registerBaseServiceProviders(){$this->register(newEventServiceProvider($this));$this->register(newLogServiceProvider($this));$this->register(newRoutingServiceProvider($this));}/***注册所有配置的提供者。**@returnvoid*/publicfunctionregisterConfiguredProviders(){(newProviderRepository($this,newFilesystem,$this->getCachedServicesPath()))->load($this->config['app.providers']);}2)。然后看到/config/app.php中注册了一个路由相关的App\Providers\RouteServiceProvider::class。它是做什么用的?这里先回答App\Providers\RouteServiceProvider继承自Illuminate\Foundation\Support\Providers\RouteServiceProvider;(并且调用我们上面的Illuminate\Support\Facades\Route,可以使用Route::*)直接看Illuminate\Foundation\Support\Providers\RouteServiceProvider你就明白了publicfunctionboot(){$this->setRootControllerNamespace();如果($this->app->routesAreCached()){$this->loadCachedRoutes();}else{$this->loadRoutes();$this->app->booted(function(){$this->app['router']->getRoutes()->refreshNameLookups();$this->app['router']->getRoutes()->refreshActionLookups();});}}publicfunctionregister(){//此处未注册}Providers文档中的boot方法是在所有服务提供者注册后调用的方法,所以这是启动后注册路由的提供者
