5

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 post How to call a console command in web application action in Yii?

Since Yii 2.0 structure is very different from Yii 1.1 ,I get errors if I try to include command/userReportController.php .Can someone guide me on this?

1
  • 1
    Thanks but I have already gone through the docs and setup the console command, my question was how can I call this from web (frontend). Aug 21, 2014 at 0:48

6 Answers 6

5

You should use an extension like https://github.com/vova07/yii2-console-runner-extension

3
  • Thanks , this is indeed helpful. Aug 21, 2014 at 0:45
  • How have you set up the console runner? Whenever I call it from my web app, it hangs and the browser keeps loading the page forever. I can't open any other page until I restart apache. Jun 3, 2015 at 18:10
  • Console Runner uses PHP_BINDIR, which points do c:\php, but I use WAMP, so php bin dir is somewhere else. I managed to fix that, but still console runner won't work. No matter if I try to run a valid controller action, or just type gibberish, it always returns status 1 :/ Jun 3, 2015 at 19:43
5

I think this is the simplest solution:

$controller = new YourConsoleController(Yii::$app->controller->id, Yii::$app);
$controller->actionYourConsoleAction();
1
  • It is indeed, but it may not work without defining the standard constants from yii\console\Application: STDIN, STDOUT and STDERR. You may need to define them. Nov 26, 2019 at 12:04
1

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')
);
1

Let's say your action is called file/convert, and you want to run it in the background:

$cmd = "cd /home/site.com/public_html/yii && nohup php yii file/convert >/dev/null &";
shell_exec( $cmd );
0

yii2-console-runner-extension keeps loading.

Try this:

It's yii (without ext, not yii.bat)

Make sure that php folder added to PATH variable (Windows)

$op = shell_exec ( 'absolute/path/to/yii ' . 'cache/flush-all' );
\yii\helpers\VarDumper::dump($op, 10, 1);

Output:

The following cache components were processed:

* cache (yii\\caching\\FileCache)
0

If you really want to run Console controller by Web controller likes migrate:

public function actionMigrate()
{
    // Keep current application
    $oldApp = \Yii::$app;
    // Load Console Application config
    $config = require \Yii::getAlias('@app'). '/config/console.php';
    new \yii\console\Application($config);
    $result = \Yii::$app->runAction('migrate', ['migrationPath' => '@app/migrations/', 'interactive' => false]);
    // Revert application
    \Yii::$app = $oldApp;
    return;
}

Above sample code is for yii2-app-basic template, you could change path for yii2-app-advanced template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.