Answer the question
In order to leave comments, you need to log in
How to call method from behavior on controller initialization in Yii2?
The behavior declares a method to be executed in the controller when it is initialized.
Behavior code:
<?php
namespace app\behaviors;
use yii\base\Behavior;
use yii\helpers\BaseFileHelper;
use app\models\Module;
class ModuleDispatcherBehavior extends Behavior {
private function updateModules() {
// ...
}
}
updateModules()
in init
:<?php
namespace app\commands;
use Yii;
use yii\console\Controller;
use mgcode\helpers\TimeHelper;
use app\behaviors\ModuleDispatcherBehavior;
class ModuleDispatcherController extends Controller
{
public function behaviors() {
return [
ModuleDispatcherBehavior::className()
];
}
// ...
public function init() {
parent::init();
$this->updateModules();
}
}
updateModules
does not exist?
Answer the question
In order to leave comments, you need to log in
The updateModules() method is declared private. Declare it as public and everything will work for you.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question