依賴注入

ThinkPHP的依賴注入(也稱之為控制反轉(zhuǎn))是一種較為輕量的實(shí)現(xiàn),無需任何的配置,并且主要針對訪問控制器進(jìn)行依賴注入??梢栽诳刂破鞯臉?gòu)造函數(shù)或者操作方法(指訪問請求的方法)中類型聲明任何(對象類型)依賴,這些依賴會被自動解析并注入到控制器實(shí)例或方法中。

自動注入請求對象

架構(gòu)方法注入

在控制器的架構(gòu)方法中會自動注入當(dāng)前請求對象,例如:

namespace app\index\controller;

use think\Request;

class Index
{
	protected $request;
    
	public function __construct(Request $request)
    {
    	$this->request = $request;
    }
    
    public function hello()
    {
        return 'Hello,' . $this->request->param('name') . '!';
    }
    
}

操作方法注入

控制器的操作方法中如果需要調(diào)用請求對象Request的話,可以在方法中定義Request類型的參數(shù),并且參數(shù)順序無關(guān),例如:

namespace app\index\controller;

use think\Request;

class Index
{

    public function hello(Request $request)
    {
        return 'Hello,' . $request->param('name') . '!';
    }
    
}

訪問URL地址的時候 無需傳入request參數(shù),系統(tǒng)會自動注入當(dāng)前的Request對象實(shí)例到該參數(shù)。

如果繼承了系統(tǒng)的Controller類的話,也可以直接調(diào)用request屬性,例如:

<?php
namespace app\index\controller;

use think\Controller;

class Index extends Controller
{

    public function hello()
    {
        return 'Hello,'.$this->request->param('name');
    }
    
}

其它對象自動注入(V5.0.1

5.0.1版本開始,控制器的架構(gòu)方法和操作方法支持任意對象的自動注入。

架構(gòu)方法注入

namespace app\index\controller;

use app\index\model\User;
use think\Request;

class Index
{
	protected $request;
    protected $user;
    
	public function __construct(Request $request, User $user)
    {
    	$this->request = $request;
        $this->user = $user;
    }
    
}

對于已經(jīng)進(jìn)行了綁定(屬性注入)的對象,即可自動完成依賴注入,如果沒有進(jìn)行對象綁定的話,會自動實(shí)例化一個新的對象示例傳入(如果類定義有instance方法,則會自動調(diào)用instance方法進(jìn)行實(shí)例化)。

架構(gòu)方法的依賴注入不影響其它類型的參數(shù)綁定。

操作方法注入

我們把User模型綁定到當(dāng)前請求對象:

Request::instance()->bind('user', \app\index\model\User::get(1));

然后就可以在操作方法中進(jìn)行對象參數(shù)的自動注入,代碼:

<?php
namespace app\index\controller;

use app\index\model\User;
use think\Controller;

class Index extends Controller
{

    public function hello(User $user)
    {
        return 'Hello,'.$user->name;
    }
    
}

如果沒有事先在Request對象中進(jìn)行對象綁定的話,調(diào)用hello方法的時候user參數(shù)會自動實(shí)例化,相當(dāng)于完成了下面的綁定操作:

Request::instance()->bind('user', new \app\index\model\User);

對象自動注入不影響原來的參數(shù)綁定。

invoke方法自動調(diào)用(v5.0.2

5.0.2版本開始,如果依賴注入的類有定義一個可調(diào)用的靜態(tài)invoke方法,則會自動調(diào)用invoke方法完成依賴注入的自動實(shí)例化。

invoke方法的參數(shù)是當(dāng)前請求對象實(shí)例,例如:

namespace app\index\model;

use think\Model;
class User extends Model
{
	public static function invoke(Request $request)
    {
    	$id = $request->param('id');
        return User::get($id);
    }
}
文檔最后更新時間:2018-04-26 09:23:10

文檔
目錄

深色
模式

切換
寬度