摘要:配置項(xiàng)即時載入的服務(wù)提供者根目錄所有即時載入的服務(wù)注冊完成之后,會立即調(diào)用方法,并標(biāo)記為已載入。最終就是將數(shù)組設(shè)置給的屬性將運(yùn)用到所有的路由,再加載。
Laravel 配置項(xiàng)即時載入的服務(wù)提供者 (根目錄:/var/www/laravel/)
所有即時載入的服務(wù)注冊完成之后,會立即調(diào)用 register 方法,并標(biāo)記為已載入。 隨后通過 IlluminateFoundationBootstrapBootProviders 啟動項(xiàng)來調(diào)用所有的即時加載服務(wù)提供者的 boot 方法 查看根據(jù) config/app.php 中的 providers 生成的 bootstrap/cache/services.php "eager" => array ( // 系統(tǒng)服務(wù)提供者 0 => "IlluminateAuthAuthServiceProvider", // 注入驗(yàn)證相關(guān)對象 1 => "IlluminateCookieCookieServiceProvider", // 注入cookie對象 2 => "IlluminateDatabaseDatabaseServiceProvider", // 注入db相關(guān)對象 3 => "IlluminateEncryptionEncryptionServiceProvider", // 注入加解密對象 4 => "IlluminateFilesystemFilesystemServiceProvider", // 注入文件相關(guān)對象 5 => "IlluminateFoundationProvidersFoundationServiceProvider",// 注入基礎(chǔ)的請求對象 6 => "IlluminateNotificationsNotificationServiceProvider", // 注入通知對象 7 => "IlluminatePaginationPaginationServiceProvider", // 注入分頁相關(guān)對象 8 => "IlluminateSessionSessionServiceProvider", // 注入session相關(guān)對象 9 => "IlluminateViewViewServiceProvider", // 注入視圖相關(guān)對象 10 => "LaravelPassportPassportServiceProvider", // 注入passport相關(guān)對象 // 配置項(xiàng)服務(wù)提供者 11 => "AppProvidersAppServiceProvider", 12 => "AppProvidersAuthServiceProvider", 13 => "AppProvidersEventServiceProvider", 14 => "AppProvidersRouteServiceProvider", )路由相關(guān)服務(wù)提供者
// 主要是注冊完之后的 boot 方法調(diào)用 AppProvidersRouteServiceProvider public function boot() { // 可以加入自己的操作 parent::boot(); } public function boot() { $this->setRootControllerNamespace(); // 若執(zhí)行了 php artisan routes:cache(自動生成路由緩存文件,注意:建議只在項(xiàng)目上線時操作),直接加載 if ($this->app->routesAreCached()) { $this->loadCachedRoutes(); } else { // 加載路由 $this->loadRoutes(); // 設(shè)置系統(tǒng)啟動時的事件監(jiān)聽函數(shù) $this->app->booted(function () { $this->app["router"]->getRoutes()->refreshNameLookups(); }); } } protected function setRootControllerNamespace() { if (! is_null($this->namespace)) { // 設(shè)置 $this->instances["url"] (IlluminateRoutingUrlGenerator對象)的 rootNamespace 屬性 $this->app[UrlGenerator::class]->setRootControllerNamespace($this->namespace); } } public function routesAreCached() { return $this["files"]->exists($this->getCachedRoutesPath()); } public function getCachedRoutesPath() { return $this->bootstrapPath()."/cache/routes.php"; } protected function loadRoutes() { if (method_exists($this, "map")) { $this->app->call([$this, "map"]); } } public function map() { $this->mapApiRoutes(); $this->mapWebRoutes(); } // API 相關(guān)的路由 protected function mapApiRoutes() { Route::prefix("api") ->middleware("api") ->namespace($this->namespace) ->group(base_path("routes/api.php")); } // WEB 相關(guān)的路由 protected function mapWebRoutes() { Route::middleware("web") ->namespace($this->namespace) ->group(base_path("routes/web.php")); } public function booted($callback) { $this->bootedCallbacks[] = $callback; // 如果應(yīng)用已經(jīng)啟動了,則直接調(diào)用 if ($this->isBooted()) { $this->fireAppCallbacks([$callback]); } } // Route 是 Facde 的調(diào)用方式 分析: Route::middleware("web") ->namespace($this->namespace) ->group(base_path("routes/web.php")); Route::middleware public static function __callStatic($method, $args) { // 獲取應(yīng)用的 router 對象 $instance = static::getFacadeRoot(); if (! $instance) { throw new RuntimeException("A facade root has not been set."); } // 將 Router::middleware() 轉(zhuǎn)化為應(yīng)用的 Router->middleware() 方式,若 Router 沒有 middleware 方法,則直接觸發(fā) __call return $instance->$method(...$args); } public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } // 將不存在的方法的調(diào)用委托給 RouteRegistrar 處理, return (new RouteRegistrar($this))->attribute($method, $parameters[0]); } public function attribute($key, $value) { // 只允許指定的屬性設(shè)置($allowedAttributes = ["as", "domain", "middleware", "name", "namespace", "prefix",]) if (! in_array($key, $this->allowedAttributes)) { throw new InvalidArgumentException("Attribute [{$key}] does not exist."); } // 支持.方式來存放屬性 $this->attributes[array_get($this->aliases, $key, $key)] = $value; return $this; } public function group($callback) { $this->router->group($this->attributes, $callback); } public function group(array $attributes, $routes) { $this->updateGroupStack($attributes); $this->loadRoutes($routes); array_pop($this->groupStack); } protected function loadRoutes($routes) { if ($routes instanceof Closure) { $routes($this); } else { $router = $this; require $routes; } }
小結(jié)
Route::middleware("web") ->namespace($this->namespace) ->group(base_path("routes/web.php")); 實(shí)際就是將 middleware namespace 加入到 RouteRegistrar 對象里面的 attributes 數(shù)組屬性,并返回 RouteRegistrar 對象,再調(diào)用 RouteRegistrar 對象的 group 方法,也就是調(diào)用 router 對象的 group 方法。最終就是將["middleware" => "web", "namespace" => $this->namespace] 數(shù)組設(shè)置給 $this->router 的groupStack 屬性(將運(yùn)用到所有的路由),再加載 config/web.php。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/22626.html
摘要:為了一探究竟,于是開啟了這次應(yīng)用性能調(diào)優(yōu)之旅。使用即時編譯器和都能輕輕松松的讓你的應(yīng)用程序在不用做任何修改的情況下,直接提高或者更高的性能。 這是一份事后的總結(jié)。在經(jīng)歷了調(diào)優(yōu)過程踩的很多坑之后,我們最終完善并實(shí)施了初步的性能測試方案,通過真實(shí)的測試數(shù)據(jù)歸納出了 Laravel 開發(fā)過程中的一些實(shí)踐技巧。 0x00 源起 最近有同事反饋 Laravel 寫的應(yīng)用程序響應(yīng)有點(diǎn)慢、20幾個并...
摘要:實(shí)例化各服務(wù)提供者,根據(jù)其屬性將服務(wù)進(jìn)行分類延遲服務(wù)即時服務(wù),從而得到一個數(shù)組格式如,延遲處理注冊延遲的服務(wù),以后再進(jìn)行調(diào)用注冊延遲的事件即時處理直接進(jìn)行注冊調(diào)用等,并重新寫入到,然后根據(jù)此文件進(jìn)行相應(yīng)的處理。 Laravel Kernel引導(dǎo)流程分析 代碼展示 protected function sendRequestThroughRouter($request) { # ...
摘要:問題分析通過閱讀源碼發(fā)現(xiàn),中的服務(wù)都是按需綁定并加載。在服務(wù)按需綁定并加載的時候,使用了類似組件的形式通過載入配置項(xiàng)并綁定服務(wù)。因?yàn)樵谶@個時候的相關(guān)配置文件還沒有被載入。 問題描述 公司一個高并發(fā)API需要從Laravel移植到Lumen,由于數(shù)據(jù)庫配置信息是通過遠(yuǎn)程或者緩存讀取后動態(tài)配置,所以在中間件時使用到了 Config::set 然而實(shí)際運(yùn)行時發(fā)現(xiàn)數(shù)據(jù)庫配置并沒有更新。 由于是...
摘要:使用即時編譯器和都能輕輕松松的讓你的應(yīng)用程序在不用做任何修改的情況下,直接提高或者更高的性能,之前做個一個實(shí)驗(yàn),具體請見使用提升程序性能。 本文經(jīng)授權(quán)轉(zhuǎn)自 PHPHub 社區(qū) 說明 性能一直是 Laravel 框架為人詬病的一個點(diǎn),所以調(diào)優(yōu) Laravel 程序算是一個必學(xué)的技能。 接下來分享一些開發(fā)的最佳實(shí)踐,還有調(diào)優(yōu)技巧,大家有別的建議也歡迎留言討論。 這里是簡單的列表: 配置信...
摘要:今天,讓我們深入研究下的廣播系統(tǒng)。廣播系統(tǒng)的目的是用于實(shí)現(xiàn)當(dāng)服務(wù)端完成某種特定功能后向客戶端推送消息的功能。這種使用場景可以完美詮釋廣播系統(tǒng)的工作原理。另外,本教程將使用廣播系統(tǒng)實(shí)現(xiàn)這樣一個即時通信應(yīng)用。 這是一篇譯文,譯文首發(fā)于 Laravel 廣播系統(tǒng)工作原理,轉(zhuǎn)載請注明出處。 今天,讓我們深入研究下 Laravel 的廣播系統(tǒng)。廣播系統(tǒng)的目的是用于實(shí)現(xiàn)當(dāng)服務(wù)端完成某種特定功能后向...
閱讀 2207·2023-04-25 15:00
閱讀 2388·2021-11-18 13:14
閱讀 1245·2021-11-15 11:37
閱讀 3130·2021-09-24 13:55
閱讀 1256·2019-08-30 15:52
閱讀 2672·2019-08-29 12:35
閱讀 3389·2019-08-29 11:04
閱讀 1237·2019-08-26 12:13