How can I call a console command in web application in Yii 2.0

后端 未结 5 931
余生分开走
余生分开走 2020-12-19 13:37

I have a console command to generate user report. I want to call the same from my web application. I am using Yii 2.0.0 beta version.I tried to follow answers given in this

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 13:51

    use this code:

    $application = new yii\console\Application($config);
    $application->runAction('controller/action');
    

    I'm using this method instead of yii console command, because I'm running Yii on managed VPS where unix commands are not supported in cron, only php scripts.

    To run it this way instead of console, the yii configuration must be initialized first, of course:

    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_ENV') or define('YII_ENV', 'dev');
    
    require(__DIR__ . '/vendor/autoload.php');
    require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
    require(__DIR__ . '/common/config/bootstrap.php');
    require(__DIR__ . '/console/config/bootstrap.php');
    
    $config = yii\helpers\ArrayHelper::merge(
        require(__DIR__ . '/common/config/main.php'),
        require(__DIR__ . '/common/config/main-local.php'),
        require(__DIR__ . '/console/config/main.php'),
        require(__DIR__ . '/console/config/main-local.php')
    );
    

提交回复
热议问题