前言
眾所周知,php的框架數(shù)不勝數(shù),近幾年,一個以優(yōu)雅著稱的框架,漸漸被國內(nèi)phper所知道,并且開始使用,但是larave有一個很明顯的缺點(diǎn)就是,他的文檔內(nèi)容少的可憐。
本文將給大家詳細(xì)介紹關(guān)于Laravel依賴注入的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
在 Laravel 的控制器的構(gòu)造方法或者成員方法,都可以通過類型約束的方式使用依賴注入,如:
html' target='_blank'>public function store(Request $request) //TODO}
這里 $request 參數(shù)就使用了類型約束,Request 是一個類:/Illuminate/Http/Request,表示參數(shù)必須是這個類或子類。
本文通過分析 Laravel 的源碼,看為什么方法中不需要傳入實例就可以直接使用 Request 呢?只是框架自動幫我們實例化并傳參了。
1.路由定義
從源頭開始看起,在路由定義文件中定義了這么一個路由:
Route::resource( /role , Admin/RoleController
這是一個資源型的路由,Laravel 會自動生成增刪改查的路由入口。
本文開頭的 store 方法就是一個控制器的方法,圖中可見路由定義的 Action 也是:App/Http/Controllers/Admin/RoleController@store
路由方法解析
根據(jù)路由定義找到控制器和方法,執(zhí)行具體的方法在 dispatch 方法中實現(xiàn)。
(文件:vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php)
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));}
首先 resolveClassMethodDependencies 方法,“顧名思義”是根據(jù)類的方法參數(shù)獲取依賴對象,然后再調(diào)用類的方法并把對象參數(shù)注入。
如果有多個依賴對象,也會 foreach 依次解析出來作為參數(shù)注入。
獲取依賴對象示例的代碼:
protected function resolveClassMethodDependencies(array $parameters, $instance, $method) if (! method_exists($instance, $method)) { return $parameters; return $this- resolveMethodDependencies( $parameters, new ReflectionMethod($instance, $method)}
這里重點(diǎn)就是用到了 PHP 的反射,注意 RelectionMethod 方法,它獲取到類的方法參數(shù)列表,可以知道參數(shù)的類型約束,參數(shù)名稱等等。
這里的 $instance 參數(shù)就是 RoleController 控制器類,$method 參數(shù)就是方法名稱 strore.
2.獲取依賴對象的示例
從方法的參數(shù)中獲取了依賴對象的約束類型,就可以實例化這個依賴的對象。
protected function transformDependency(ReflectionParameter $parameter, $parameters) $class = $parameter- getClass(); // If the parameter has a type-hinted class, we will check to see if it is already in // the list of parameters. If it is we will just skip it as it is probably a model // binding and we do not want to mess with those; otherwise, we resolve it here. if ($class ! $this- alreadyInParameters($class- name, $parameters)) { return $parameter- isDefaultValueAvailable() ? $parameter- getDefaultValue() : $this- container- make($class- name);}
根據(jù)類名從容器中獲取對象,這個綁定對象實例的過程在服務(wù)提供者中先定義和了。
然后把實例化的對象傳入到 store 方法中,就可以使用依賴的對象了。
3.關(guān)于 PHP 反射
舉個使用 ReflectionMethod 的例子。
class Demo private $request; public function store(Request $request)}
打印出 new ReflectionMethod(Demo::class, ‘store ) 的內(nèi)容如圖:
可以得出這個方法的參數(shù)列表,參數(shù)的約束類型,如 typeHint,Illuminate/Http/Request.
根據(jù)類名可以從容器中獲取一開始通過服務(wù)提供者綁定的實例。
總結(jié)
您可能感興趣的文章:php數(shù)據(jù)結(jié)構(gòu)之順序鏈表與鏈?zhǔn)骄€性表的示例
基于php中echo用逗號和用點(diǎn)號的區(qū)別詳細(xì)講解
php實現(xiàn)統(tǒng)計二進(jìn)制中1的個數(shù)算法的示例
以上就是通過源碼來解析Laravel依賴注入的相關(guān)內(nèi)容的詳細(xì)內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答