Running Console Commands From Web

Question to yii dev team & yii gurus - probably very basic.

Is it possible in some way for running console commands from web (does it need [font=“Courier New”]shell_exec[/font] or something)? If so, what’s the best practice?

For example can db migrations be run via web interface? Examples may help.

Yes, it is possible and not via shell_exec. Check console controller code and you’ll get an idea. Don’t have enough time to compose full featured example now :(

I am looking for a similar requirement in my app. Will be good if it is there as a wiki or doc?




$oldApp = \Yii::$app;

new \yii\console\Application([

    'id' => 'Command runner',

    'basePath' => '@app',

    'components' => [

        'db' => $oldApp->db,

    ],

);

\Yii::$app->runAction('migrate/up', ['migrationPath' => '@yii/rbac/migrations/', 'interactive' => false]);

\Yii:$app = $oldApp;



Thanks samdark will attempt this.

Your code the only example that I found telling how to run console commands from web. But I can’t put it working.

Can you show me an example how to run the "actionIndex" from "commands/HelloController.php".

For a better result; it’s better than the newly created console application; must have all the configuration parameters in the file:

 console\config\main.php

So this could encapsulate:

<?php

namespace console\components;
use yii\base\Component;
use yii\helpers\ArrayHelper;
Class Command extends Component{    
    const ROUTE_CONFIG_COMMON=__DIR__.'/../../common';
    const ROUTE_CONFIG_CONSOLE=  __DIR__.'/../config';


public static function execute($action,$params){
    $oldApp = \Yii::$app;
    $newApp=static::getNewApp(static::getConfig());
    $newApp->runAction($action,$params);
     \Yii::$app = $oldApp; 
     return true;
}

private static function getConfig(){
    $config= ArrayHelper::merge(
require static::ROUTE_CONFIG_COMMON.'/config/main.php',
require static::ROUTE_CONFIG_COMMON.'/config/main-local.php',
require static::ROUTE_CONFIG_CONSOLE.'/main.php',
require static::ROUTE_CONFIG_CONSOLE.'/main-local.php'   
    );
    $config['components']['db']=\Yii::$app->db;
    return $config;
}

private static function getNewApp($config){       
return new \yii\console\Application($config);           
}

}

Then you could execute any command, in the simplest way:

use console\components\Command

Command::execute('migrate/up', ['interactive' => false]);

Regards

1 Like

thanks! how i can run this command in background?

I want to do the same thing, but my component is bootstraped. After running console application, error handler is not working.

To reproduce the problem, just create a bootstraped component and run the following code in its bootstrap method:

        $oldApp = \Yii::$app;
        new Application([
            'id' => 'console-app',
            'basePath' => '@app',
        ]);
        \Yii::$app = $oldApp;

After browsing to a non existence route, the not-found error is not rendered in the view, just simple text.

Please help me