A
A
artem782017-07-26 02:16:10
Yii
artem78, 2017-07-26 02:16:10

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() {
        // ...
    }
}

In the controller I call 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();
    }
}

Why in this case do I get an error that the method updateModulesdoes not exist?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Ivanov, 2017-07-26
@artem78

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 question

Ask a Question

731 491 924 answers to any question