摘要:入口啟動(dòng)后,會(huì)先加載服務(wù)提供者中間件等組件,在查找路由之前因?yàn)槲覀兪褂玫氖情T面,所以先要查到的實(shí)體類。注冊第一步當(dāng)然還是通過服務(wù)提供者,因?yàn)檫@是啟動(dòng)的關(guān)鍵,在內(nèi)加載路由文件。因路由文件中沒有命名空間。
前言
我的解析文章并非深層次多領(lǐng)域的解析攻略。但是參考著開發(fā)文檔看此類文章會(huì)讓你在日常開發(fā)中更上一層樓。
廢話不多說,我們開始本章的講解。
入口Laravel啟動(dòng)后,會(huì)先加載服務(wù)提供者、中間件等組件,在查找路由之前因?yàn)槲覀兪褂玫氖情T面,所以先要查到Route的實(shí)體類。
注冊第一步當(dāng)然還是通過服務(wù)提供者,因?yàn)檫@是laravel啟動(dòng)的關(guān)鍵,在 RouteServiceProvider 內(nèi)加載路由文件。
protected function mapApiRoutes() { Route::prefix("api") ->middleware("api") ->namespace($this->namespace) // 設(shè)置所處命名空間 ->group(base_path("routes/api.php")); //所得路由文件絕對(duì)路徑 }
首先require是不可缺少的。因路由文件中沒有命名空間。 IlluminateRoutingRouter 下方法
protected function loadRoutes($routes) { if ($routes instanceof Closure) { $routes($this); } else { $router = $this; require $routes; } }
隨后通過路由找到指定方法,依舊是 IlluminateRoutingRouter 內(nèi)有你所使用的所有路由相關(guān)方法,例如get、post、put、patch等等,他們都調(diào)用了統(tǒng)一的方法 addRoute
public function addRoute($methods, $uri, $action) { return $this->routes->add($this->createRoute($methods, $uri, $action)); }
之后通過 IlluminateRoutingRouteCollection addToCollections 方法添加到集合中
protected function addToCollections($route) { $domainAndUri = $route->getDomain().$route->uri(); foreach ($route->methods() as $method) { $this->routes[$method][$domainAndUri] = $route; } $this->allRoutes[$method.$domainAndUri] = $route; }
添加后的結(jié)果如下圖所示
通過 IlluminateRoutingRouter 方法開始運(yùn)行路由實(shí)例化的邏輯
protected function runRoute(Request $request, Route $route) { $request->setRouteResolver(function () use ($route) { return $route; }); $this->events->dispatch(new EventsRouteMatched($route, $request)); return $this->prepareResponse($request, $this->runRouteWithinStack($route, $request) ); } .... protected function runRouteWithinStack(Route $route, Request $request) { $shouldSkipMiddleware = $this->container->bound("middleware.disable") && $this->container->make("middleware.disable") === true; $middleware = $shouldSkipMiddleware ? [] : $this->gatherRouteMiddleware($route); return (new Pipeline($this->container)) ->send($request) ->through($middleware) ->then(function ($request) use ($route) { return $this->prepareResponse( $request, $route->run() // 此處調(diào)用run方法 ); }); }
在 IlluminateRoutingRoute 下 run 方用于執(zhí)行控制器的方法
public function run() { $this->container = $this->container ?: new Container; try { if ($this->isControllerAction()) { return $this->runController(); //運(yùn)行一個(gè)路由并作出響應(yīng) } return $this->runCallable(); } catch (HttpResponseException $e) { return $e->getResponse(); } }
從上述方法內(nèi)可以看出 runController 是運(yùn)行路由的關(guān)鍵,方法內(nèi)運(yùn)行了一個(gè)調(diào)度程序,將控制器 $this->getController() 和控制器方法 $this->getControllerMethod() 傳入到 dispatch 調(diào)度方法內(nèi)
protected function runController() { return $this->controllerDispatcher()->dispatch( $this, $this->getController(), $this->getControllerMethod() ); }
這里注意 getController() 才是真正的將控制器實(shí)例化的方法
public function getController() { if (! $this->controller) { $class = $this->parseControllerCallback()[0]; // 0=>控制器 xxController 1=>方法名 index $this->controller = $this->container->make(ltrim($class, "")); // 交給容器進(jìn)行反射 } return $this->controller; }實(shí)例化
依舊通過反射加載路由指定的控制器,這個(gè)時(shí)候build的參數(shù)$concrete = AppApiControllersXxxController
public function build($concrete) { // If the concrete type is actually a Closure, we will just execute it and // hand back the results of the functions, which allows functions to be // used as resolvers for more fine-tuned resolution of these objects. if ($concrete instanceof Closure) { return $concrete($this, $this->getLastParameterOverride()); } $reflector = new ReflectionClass($concrete); // If the type is not instantiable, the developer is attempting to resolve // an abstract type such as an Interface of Abstract Class and there is // no binding registered for the abstractions so we need to bail out. if (! $reflector->isInstantiable()) { return $this->notInstantiable($concrete); } $this->buildStack[] = $concrete; $constructor = $reflector->getConstructor(); // If there are no constructors, that means there are no dependencies then // we can just resolve the instances of the objects right away, without // resolving any other types or dependencies out of these containers. if (is_null($constructor)) { array_pop($this->buildStack); return new $concrete; } $dependencies = $constructor->getParameters(); // Once we have all the constructor"s parameters we can create each of the // dependency instances and then use the reflection instances to make a // new instance of this class, injecting the created dependencies in. $instances = $this->resolveDependencies( $dependencies ); array_pop($this->buildStack); return $reflector->newInstanceArgs($instances); }
這時(shí)將返回控制器的實(shí)例,下面將通過url訪問指定方法,一般控制器都會(huì)繼承父類 IlluminateRoutingController ,laravel為其設(shè)置了別名 BaseController
public function dispatch(Route $route, $controller, $method) { $parameters = $this->resolveClassMethodDependencies( $route->parametersWithoutNulls(), $controller, $method ); if (method_exists($controller, "callAction")) { return $controller->callAction($method, $parameters); } return $controller->{$method}(...array_values($parameters)); }
Laravel通過controller繼承的callAction去調(diào)用子類的指定方法,也就是我們希望調(diào)用的自定義方法。
public function callAction($method, $parameters) { return call_user_func_array([$this, $method], $parameters); }致謝
感謝你看到這里,本篇文章源碼解析靠個(gè)人理解。如有出入請(qǐng)拍磚。
希望本篇文章可以幫到你。謝謝
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/29450.html
摘要:實(shí)際上,在中關(guān)閉主要包括兩個(gè)過程保存當(dāng)前到介質(zhì)中在中存入。,學(xué)習(xí)下關(guān)閉的源碼吧先。總之,關(guān)閉的第二件事就是給添加。通過對(duì)的源碼分析可看出共分為三大步啟動(dòng)操作關(guān)閉??偨Y(jié)本小系列主要學(xué)習(xí)了的源碼,學(xué)習(xí)了的三大步。 說明:在中篇中學(xué)習(xí)了session的CRUD增刪改查操作,本篇主要學(xué)習(xí)關(guān)閉session的相關(guān)源碼。實(shí)際上,在Laravel5.3中關(guān)閉session主要包括兩個(gè)過程:保存當(dāng)前U...
摘要:總結(jié)本文主要學(xué)習(xí)了啟動(dòng)時(shí)做的七步準(zhǔn)備工作環(huán)境檢測配置加載日志配置異常處理注冊注冊啟動(dòng)。 說明:Laravel在把Request通過管道Pipeline送入中間件Middleware和路由Router之前,還做了程序的啟動(dòng)Bootstrap工作,本文主要學(xué)習(xí)相關(guān)源碼,看看Laravel啟動(dòng)程序做了哪些具體工作,并將個(gè)人的研究心得分享出來,希望對(duì)別人有所幫助。Laravel在入口index...
在 Laravel 的控制器的構(gòu)造方法或者成員方法,都可以通過類型約束的方式使用依賴注入,如: public function store(Request $request) { //TODO } 這里 $request 參數(shù)就使用了類型約束,Request 是類型約束的類型,它是一個(gè)類:IlluminateHttpRequest. 本文研究 Laravel 的依賴注入原理,為什么這樣定義...
摘要:行,是否強(qiáng)制訪問。行,嘗試處理此次請(qǐng)求,詳細(xì)見方法。至此,的執(zhí)行主流程完畢。小結(jié)是的核心文件,它被調(diào)用后,完成了諸多的主流程操作。此文可以轉(zhuǎn)載,但轉(zhuǎn)載前需要發(fā)郵件到進(jìn)行溝通,未溝通的均視作侵權(quán)。 前言 Codeigniter.php 是 CI 4 的核心所在,在這里接收并處理了 request 請(qǐng)求,安全檢查,緩存處理, URL 解析以及路由匹配,執(zhí)行過濾器,加載運(yùn)行 Controll...
摘要:本文將會(huì)源碼分析下是如何把開發(fā)者在中寫的路由列表注冊到對(duì)象內(nèi)的。通過以上的分析,就能對(duì)路由系統(tǒng)的基本設(shè)計(jì)越來越清晰。一個(gè)進(jìn)來后,首先開始啟動(dòng)并按照以上邏輯開始注冊路由列表,然后就是根據(jù)當(dāng)前信息查找對(duì)應(yīng)的對(duì)象。 我們知道,在 Laravel 世界里,外界傳進(jìn)來一個(gè) Request 時(shí),會(huì)被 Kernel 處理并返回給外界一個(gè) Response。Kernel 在處理 Request 時(shí),會(huì)...
閱讀 2616·2021-09-06 15:02
閱讀 3283·2021-09-02 10:18
閱讀 2888·2019-08-30 15:44
閱讀 734·2019-08-30 15:43
閱讀 2002·2019-08-30 14:08
閱讀 2807·2019-08-30 13:16
閱讀 1462·2019-08-26 13:52
閱讀 972·2019-08-26 12:21