Yii依賴注入


DI(依賴注入)容器是一個知道如何範例化和組態物件的物件。 Yii提供對它 yii\di\Container 類來提供 DI 容器。
它支援以下幾種注入方式 -
  • Setter方法和屬性注入
  • PHP可呼叫注入
  • 建構函式注入
  • 控制器動作注入
依賴注入(DI)容器支援構造器使用型別提示的幫助來注入 -
class Object1 {
   public function __construct(Object2 $object2) {

   }
}
$object1 = $container->get('Object1');
// which is equivalent to the following:
$object2 = new Object2;
$object1 = new Object1($object2);
屬性和 setter 注入需要通過組態支援 -
<?php
   use yii\base\Object;
   class MyObject extends Object {
      public $var1;
      private $_var2;
      public function getVar2() {
         return $this->_var2;
      }
      public function setVar2(MyObject2 $var2) {
         $this->_var2 = $var2;
      }
   }
   $container->get('MyObject', [], [
      'var1' => $container->get('MyOtherObject'),
      'var2' => $container->get('MyObject2'),
   ]);
?>
在呼叫PHP注入的案例中,容器將使用註冊的 PHP 回撥構建類的新範例 -
$container->set('Object1', function () {
   $object1 = new Object1(new Object2);
   return $object1;
});
$object1 = $container->get('Object1');
控制器操作注入是一種依賴注入(DI)型別,在這裡依賴宣告使用型別提示。這是有助於保持 MVC 控制器的超輕量級 -
public function actionSendToAdmin(EmailValidator $validator, $email) {
   if ($validator->validate($email)) {
      // sending email
   }
}
可以使用 yii\db\Container::set() 方法來註冊依賴 -
<?php
   $container = new \yii\di\Container;
   // register a class name as is. This can be skipped.
   $container->set('yii\db\Connection');
   // register an alias name. You can use $container->get('MyObject')
   // to create an instance of Connection
   $container->set('MyObject', 'yii\db\Connection');
   // register an interface
   // When a class depends on the interface, the corresponding class
   // will be instantiated as the dependent object
   $container->set('yii\mail\MailInterface', 'yii\swiftmailer\Mailer');
   // register an alias name with class configuration
   // In this case, a "class" element is required to specify the class
   $container->set('db', [
      'class' => 'yii\db\Connection',
      'dsn' => 'mysql:host=127.0.0.1;dbname = helloworld',
      'username' => 'vladimir',
      'password' => '12345',
      'charset' => 'utf8',
   ]);
   // register a class with configuration. The configuration
   // will be applied when the class is instantiated by get()
   $container->set('yii\db\Connection', [
      'dsn' => 'mysql:host=127.0.0.1;dbname = mystudy',
      'username' => 'root',
      'password' => '',
      'charset' => 'utf8',
   ]);
   // register a PHP callable
   // The callable will be executed each time when $container->get('db') is called
   $container->set('db', function ($container, $params, $config) {
      return new \yii\db\Connection($config);
   });
   // register a component instance
   // $container->get('pageCache') will return the same instance each time when it 
      //is called
   $container->set('pageCache', new FileCache);
?>

使用依賴注入(DI)

第1步 - 在 components 檔案夾中建立一個 MyInterface.php 的檔案,並使用下面的程式碼。
<?php
   namespace app\components;
   interface MyInterface {
      public function test();
   }
?>
第2步 - 在 components 檔案夾中建立兩個檔案。

First.php ?

<?php
   namespace app\components;
   use app\components\MyInterface;
   class First implements MyInterface {
      public function test() {
         echo "First class <br>";
      }
   }
?>

Second.php ?

<?php
   namespace app\components;
   use app\components\MyInterface;
      class Second implements MyInterface {
      public function test() {
         echo "Second class <br>";
      }
   }
?>
第3步 - 現在,新增一個 actionTestInterface 到控制器 SiteController。
public function actionTestInterface() {
   $container = new \yii\di\Container();
   $container->set
      ("\app\components\MyInterface","\app\components\First");
   $obj = $container->get("\app\components\MyInterface");
   $obj->test(); // print "First class"
   $container->set
      ("\app\components\MyInterface","\app\components\Second");
   $obj = $container->get("\app\components\MyInterface");
   $obj->test(); // print "Second class"
}
第4步 - 在瀏覽器開啟URL地址:http://localhost:8080/index.php?r=site/test-interface ,會看到輸出如下圖所示。
Yii依賴注入

這種方法很方便,因為我們可以在一個地方設定類,而且其他程式碼將自動使用新類。