如何从网络在 yii2 中运行控制台命令

2022-01-03 00:00:00 shell cron console php yii2

我在 console/controllers 中创建了一个带有 SuggestionController 的控制台命令.

I have created a console command in console/controllers with SuggestionController .

如果我运行像 php yii Recommendation 这样的命令,它就可以工作了.

If i run command like php yii suggestions, its working.

我想知道如何在没有任何 yii2 扩展的情况下从 web 执行控制台命令.

I want to know how to execute console command from web without any extensions of yii2.

推荐答案

可以做得更简单

$oldApp = Yii::$app;
new yiiconsoleApplication([
    'id' => 'Command runner',
    'basePath' => '@app',
    'components' => [
        'db' => $oldApp->db,
    ],
);
Yii::$app->runAction('migrate/up', ['migrationPath' => '@yii/rbac/migrations/', 'interactive' => false]);
Yii:$app = $oldApp;

Github 链接

相关文章