php - DI in yii2, constructor injection -
i have created interface, i'm trying inject inside controller. i'm getting following error:
argument 1 passed backend\controllers\agencycontroller::__construct() must implement interface common\service\appserviceinterface, string given
i created service folder inside common folder, added 2 files it
- appservice.php
- appserviceinterface.php
now defined dependency inside common/bootstrap.php
file below:
yii::$container->set('common\service\appserviceinterface', 'common\service\appservice');
afterwards tried inject inside agencycontroller placed inside backend/controllers/agencycontroller below:
namespace backend\controllers; use common\service\appserviceinterface; public function __construct(appserviceinterface $appservice) { $this->appservice = $appservice; }
but i'm getting error mentioned earlier.
so have change __construct method below , working fine:
public function __construct($id, $module, appserviceinterface $appservice , $config = []) { parent::__construct($id, $module); $this->appservice = $appservice; }
Comments
Post a Comment