mertvetsky/yii2-simple-container-configurator

用于注册 Yii2 类定义的简单扩展

0.1 2016-09-10 23:01 UTC

This package is auto-updated.

Last update: 2024-09-20 21:37:30 UTC


README

只需将以下内容添加到 components

        'containerConfig' => [
             'class' => \mertvetsky\yii2SimpleContainerConfigurator\SimpleContainerConfig::class,
             'file' => __DIR__ . '/services.php',
        ],

并将 'containerConfig' 添加到 bootstrap 块。

然后创建 config/services.php 文件,定义你的服务,例如

<?php

return [
    'smth' => [
        'class'     => \app\lib\smth\Smth::class,
        'singleton' => false,
        'message'   => 'from config' // or any defined as public field in your class
    ],
    'pew'  => [
        'class' => \app\lib\smth\Pewpew::class
    ]
];

之后,你可以在任何 \yii\base\Component 子类中使用你配置的类

    public function __construct($id, Module $module, Smth $smth, Pewpew $pewpew, array $config = [])
    {
        parent::__construct($id, $module, $config);
        $this->smth = $smth;
        $this->pewpew = $pewpew;
    }