Answer the question
In order to leave comments, you need to log in
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);
};
}
}
PHP Fatal Error 'yii\base\ErrorException' with message 'Class 'app\modules\events\models\EventsUser' not found'
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question