<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ThinkPHP的依賴注入(也稱之為控制反轉)是一種較為輕量的實現,無需任何的配置,并且主要針對訪問控制器進行依賴注入。可以在控制器的構造函數或者操作方法(指訪問請求的方法)中類型聲明任何(對象類型)依賴,這些依賴會被自動解析并注入到控制器實例或方法中。 ## 自動注入請求對象 ### 架構方法注入 在控制器的架構方法中會自動注入當前請求對象,例如: ~~~ 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') . '!'; } } ~~~ ### 操作方法注入 控制器的操作方法中如果需要調用請求對象`Request`的話,可以在方法中定義`Request`類型的參數,并且參數順序無關,例如: ~~~ namespace app\index\controller; use think\Request; class Index { public function hello(Request $request) { return 'Hello,' . $request->param('name') . '!'; } } ~~~ 訪問URL地址的時候 無需傳入`request`參數,系統會自動注入當前的`Request`對象實例到該參數。 如果繼承了系統的`Controller`類的話,也可以直接調用`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`版本開始,控制器的架構方法和操作方法支持任意對象的自動注入。 ### 架構方法注入 ~~~ 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; } } ~~~ > 對于已經進行了綁定(屬性注入)的對象,即可自動完成依賴注入,如果沒有進行對象綁定的話,會自動實例化一個新的對象示例傳入(如果類定義有`instance`方法,則會自動調用`instance`方法進行實例化)。 架構方法的依賴注入不影響其它類型的參數綁定。 ### 操作方法注入 我們把`User`模型綁定到當前請求對象: ~~~ Request::instance()->bind('user', \app\index\model\User::get(1)); ~~~ 然后就可以在操作方法中進行對象參數的自動注入,代碼: ~~~ <?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對象中進行對象綁定的話,調用`hello`方法的時候`user`參數會自動實例化,相當于完成了下面的綁定操作: ~~~ Request::instance()->bind('user', new \app\index\model\User); ~~~ > 對象自動注入不影響原來的參數綁定。 ## invoke方法自動調用(`v5.0.2`) 5.0.2版本開始,如果依賴注入的類有定義一個可調用的靜態`invoke`方法,則會自動調用invoke方法完成依賴注入的自動實例化。 `invoke`方法的參數是當前請求對象實例,例如: ~~~ 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); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看