前言
眾所周知,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)造方法或者成員方法,都可以通過類型約束的方式使用依賴注入,如:
public function store(Request $request)
{
//TODO
}
這里 $request 參數(shù)就使用了類型約束,Request 是一個類:\Illuminate\Http\Request,表示參數(shù)必須是這個類或子類。
本文通過分析 Laravel 的源碼,看為什么方法中不需要傳入實(shí)例就可以直接使用 Request 呢?只是框架自動幫我們實(shí)例化并傳參了。
1.路由定義
從源頭開始看起,在路由定義文件中定義了這么一個路由:
Route::resource('/role', 'Admin\RoleController');
這是一個資源型的路由,Laravel 會自動生成增刪改查的路由入口。
本文開頭的 store 方法就是一個控制器的方法,圖中可見路由定義的 Action 也是:App\Http\Controllers\Admin\RoleController@store
路由方法解析
根據(jù)路由定義找到控制器和方法,執(zhí)行具體的方法在 dispatch 方法中實(shí)現(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ù)中獲取了依賴對象的約束類型,就可以實(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ù)類名從容器中獲取對象,這個綁定對象實(shí)例的過程在服務(wù)提供者中先定義和了。
然后把實(shí)例化的對象傳入到 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ù)提供者綁定的實(shí)例。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
您可能感興趣的文章:- php+laravel依賴注入知識點(diǎn)總結(jié)
- laravel框架中你所用到的依賴注入詳解
- Laravel實(shí)現(xiàn)構(gòu)造函數(shù)自動依賴注入的方法
- PHP依賴注入容器知識點(diǎn)淺析
- php依賴注入知識點(diǎn)詳解
- php中的依賴注入實(shí)例詳解
- php反射學(xué)習(xí)之依賴注入示例
- PHP依賴注入原理與用法分析
- 詳解Laravel框架的依賴注入功能