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.
samdark
(Alexander Makarov)
May 1, 2014, 7:52am
2
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
langy
(Kilo Voltage)
May 4, 2014, 6:24am
3
I am looking for a similar requirement in my app. Will be good if it is there as a wiki or doc?
samdark
(Alexander Makarov)
May 6, 2014, 9:02pm
4
$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;
langy
(Kilo Voltage)
May 7, 2014, 4:28am
5
Thanks samdark will attempt this.
kandalf
(Kandalf01)
October 22, 2014, 11:01pm
6
samdark:
$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;
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".
Geus
(Geus)
April 3, 2019, 7:29pm
7
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?
R.S.R
(Reza Saberi Rad)
September 26, 2019, 2:01pm
9
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