L
L
Lander2015-08-21 13:11:27
Yii
Lander, 2015-08-21 13:11:27

How to change namespace on the fly?

Good afternoon!
Maybe a noob question, but I'm lost. I don't even know how to google it.
There is a project on Yii2 on an advanced template. The frontend has modules with a namespace: It is necessary to go through the modules by cron and call actionCron from the controller if it is implemented. Since it is dangerous to call such actions with wget (the limit is 30 seconds), a CronController is created in the console with all the consequences of the type namespace console\controllers. controllerMap (for calling via run()) would not be desirable to use, since the set of modules is not known in advance. I resorted to this trick:
namespace app\modules\<имя модуля>\...;

public function actionExec()
  {
    $plugins = \common\models\Plugins::find()->all();
    foreach ($plugins as $plugin) {
      $controller = 'frontend\\modules\\' . $plugin->name . '\\controllers\\CronController';
      if (class_exists($controller))
      {
        $obj = new $controller($plugin->name, $plugin->name);
        $result = $obj->actionIndex($plugin);
      };
    }
  }

The calls pass, but here are the internal calls to models that have app\modules\... as their namespace:
PHP Fatal Error 'yii\base\ErrorException' with message 'Class 'app\modules\events\models\EventsUser' not found'

So you don't want to change all app to frontend...
Question: Is it possible to make a console application think that app is frontend? Or have I gone wrong somewhere?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-08-24
@usdglander

That is, in the directories 'frontend\modules\<module name>' , there are modules with namespaces 'app\modules\<module name>' ?
You can override ' @app ' with Yii::setAlias() .
But it's better to get the namespaces in order .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question