我已经创建了一个接口,我正试图将其注入控制器内。但我得到以下错误:
参数1传递给后端'controllers'AgencyController::__construct()必须实现接口common'service'AppServiceInterface, string given
我在common文件夹中创建了service文件夹,并添加了两个文件
- AppService.php
- AppServiceInterface.php
现在我在common/bootstrap.php
文件中定义了这个依赖项,如下所示:
Yii::$container->set('common'service'AppServiceInterface',
'common'service'AppService');
之后,我尝试将其注入到AgencyController中,该AgencyController位于backend/controllers/AgencyController中,如下所示:
namespace backend'controllers;
use common'service'AppServiceInterface;
public function __construct(AppServiceInterface $appService)
{
$this->appService = $appService;
}
但是我得到了前面提到的错误
所以我必须像下面这样改变__construct方法,它的工作很好:
public function __construct($id, $module, AppServiceInterface $appService , $config = [])
{
parent::__construct($id, $module);
$this->appService = $appService;
}