Как внедрить сервис-класс как singleton
Добавлено: 2021.04.17, 11:31
Сервис-класс
Контроллер
Хочу чтоб в localhodst/my/get вышел объект с установленным значением ('Атрибут')
Код: Выделить всё
<?php
namespace app\services;
class MyService
{
protected $attribute;
public function setAttribute($attribute)
{
$this->attribute = $attribute;
}
}
Код: Выделить всё
<?php
namespace app\controllers;
use Yii;
use app\services\MyService;
class MyController extends \yii\web\Controller
{
protected $myService;
public function __construct($id, $module, MyService $myService, $config = [])
{
$this->myService = $myService;
parent::__construct($id, $module, $config);
}
public function actionSet()
{
$this->myService->setAttribute('Атрибут');
var_dump($this->myService);
}
public function actionGet()
{
var_dump($this->myService);
}
}