- Yii 教程
 - Yii - 主页
 - Yii - 概述
 - Yii - 安装
 - Yii - 创建页面
 - Yii - 应用程序结构
 - Yii - 入口脚本
 - Yii - 控制器
 - Yii - 使用控制器
 - Yii - 使用动作
 - Yii - 模型
 - Yii - 小部件
 - Yii - 模块
 - Yii - 视图
 - Yii - 布局
 - Yii - 资产
 - Yii - 资产转换
 - Yii - 扩展
 - Yii - 创建扩展
 - Yii - HTTP 请求
 - Yii - 响应
 - Yii - URL 格式
 - Yii - URL 路由
 - Yii - URL 规则
 - Yii - HTML 表单
 - Yii - 验证
 - Yii - 临时验证
 - Yii - AJAX 验证
 - Yii - 会话
 - Yii - 使用闪存数据
 - Yii - cookie
 - Yii - 使用 Cookie
 - Yii - 文件上传
 - Yii - 格式化
 - Yii - 分页
 - Yii - 排序
 - Yii - 属性
 - Yii - 数据提供者
 - Yii - 数据小部件
 - Yii - 列表视图小部件
 - Yii - GridView 小部件
 - Yii - 活动
 - Yii - 创建事件
 - Yii - Behave
 - Yii - 创建Behave
 - Yii - 配置
 - Yii - 依赖注入
 - Yii - 数据库访问
 - Yii - 数据访问对象
 - Yii - 查询生成器
 - Yii - 活动记录
 - Yii - 数据库迁移
 - Yii - 主题化
 - Yii - RESTful API
 - Yii - RESTful API 的实际应用
 - Yii - 字段
 - Yii - 测试
 - Yii - 缓存
 - Yii - 片段缓存
 - Yii - 别名
 - Yii - 日志记录
 - Yii - 错误处理
 - Yii - 身份验证
 - Yii - 授权
 - Yii - 本地化
 - Yii-Gii
 - Gii – 创建模型
 - Gii – 生成控制器
 - Gii – 生成模块
 
- Yii 有用的资源
 - Yii - 快速指南
 - Yii - 有用的资源
 - Yii - 讨论
 
Yii - 依赖注入
DI(依赖注入)容器是一个知道如何实例化和配置对象的对象。Yii 通过yii\di\Container 类提供 DI 容器。
它支持以下类型的 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);
通过配置支持属性和设置器注入 -
<?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 = helloworld',
      'username' => 'vladimir',
      'password' => '12345',
      '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 - 在组件文件夹内创建一个名为MyInterface.php的文件,其中包含以下代码。
<?php
   namespace app\components;
   interface MyInterface {
      public function test();
   }
?>
步骤 2 - 在组件文件夹内,创建两个文件。
首先.php -
<?php
   namespace app\components;
   use app\components\MyInterface;
   class First implements MyInterface {
      public function test() {
         echo "First class <br>";
      }
   }
?>
Second.php -
<?php
   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 - 转到http://localhost:8080/index.php?r=site/test-interface您应该看到以下内容。
这种方法很方便,因为我们可以在一个地方设置类,其他代码将自动使用新类。