Yii 依赖注入基础
谁能给我解释一下基本知识吗?我理解它是什么,但我现在真的不知道如何在实践中使用DI容器。例如,我在同一控制器中有两个函数:Yii 依赖注入基础,yii,dependency-injection,dependencies,Yii,Dependency Injection,Dependencies,谁能给我解释一下基本知识吗?我理解它是什么,但我现在真的不知道如何在实践中使用DI容器。例如,我在同一控制器中有两个函数: public function actionIndex() { $productsModel = new Products(); $productsFormModel = new ProductsForm(); $informationFormModel = new InformationForm(); .... } public fun
public function actionIndex()
{
$productsModel = new Products();
$productsFormModel = new ProductsForm();
$informationFormModel = new InformationForm();
....
}
public function actionInformation()
{
$productsModel = new Products();
$productsFormModel = new ProductsForm();
$informationFormModel = new InformationForm();
....
}
所以我的两个问题是:
<?php
\Yii::$container->set('products_model', 'app\models\Products');
\Yii::$container->set('products_form', 'app\models\ProductsForm');
\Yii::$container->set('information_form', 'app\models\InformationForm');
?>
但这不是个好主意,对吗
请回答我的两个问题
谢谢
ProductsInfo
)来明确这种关系。可以相应地命名控制器(ProductsInfoController
),从而澄清应用程序结构$instance_products = \Yii::$container->get('products_model');
$instance_products_form = \Yii::$container->get('products_form');
$instance_information_form = \Yii::$container->get('information_form');