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

时间:2015-12-14 10:15:27

标签: php shell cron console yii2

我已使用console/controllersSuggestionController中创建了一个控制台命令。

如果我运行像php yii suggestions这样的命令,那就是它的工作。

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

7 个答案:

答案 0 :(得分:5)

可以做得更简单

$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;

<强> Github LINK

答案 1 :(得分:2)

这是我前段时间发现和使用的方式来运行yii控制台控制器/操作(我用它来从web运行迁移)。

在您的网络控制器操作中:

// default console commands outputs to STDOUT
// so this needs to be declared for wep app
if (! defined('STDOUT')) {
    define('STDOUT', fopen('/tmp/stdout', 'w'));
}

$consoleController = new \yii\console\controllers\SuggestionController;
$consoleController->runAction('your action eg. index');

/**
* open the STDOUT output file for reading
*
* @var $message collects the resulting messages of the migrate command to be displayed in a view
*/
$handle = fopen('/tmp/stdout', 'r');
$message = '';
while (($buffer = fgets($handle, 4096)) !== false) {
$message .= $buffer . "\n";
}
fclose($handle);

return $message;

答案 2 :(得分:1)

你可以UITableView你的命令'''yp yii suggestions'''但这可能会导致网络服务器用户的权限问题。

更好的方法是使用ConsoleRunner扩展,例如yii2-console-runner yii2-console-runner-extension使用UITableViewCells使作业控制工作更复杂,更安全。

执行exec()等时始终注意代码注入!

答案 3 :(得分:1)

自Yii2 - 2.0.11.2高级应用程序 - 这可行

首先让我们确保控制器和命名空间正确。在这种情况下,前端应用程序访问控制台应用程序导入方法()

在console \ controllers \ FhirController

enter image description here

在控制台\ config \ main.php [可选]

中设置别名可用

enter image description here

'aliases' => [
    '@common' => dirname(__DIR__),
    '@frontend' =>  dirname(dirname(__DIR__)) . '/frontend',
    '@backend' =>  dirname(dirname(__DIR__)) . '/backend',
    '@console' =>  dirname(dirname(__DIR__)) . '/console',
],

最后从前端视图中,进行如下调用: 在这种情况下,调用控制器路由fhir然后方法import()

$consoleController = new console\controllers\FhirController('fhir', Yii::$app); 
$consoleController->runAction('import');

答案 4 :(得分:1)

在我正在检修的站点上,我需要一个后台任务,该任务可以通过操作进行切换,这需要我也可以使用ps找到其pid。经过大量的搜索和几乎相同的咒骂之后,我拼凑出解决方案。

# First change to (already-calculated) correct dir:
chdir($strPath);

# Now execute with nohup, directed to dev/null, and crucially with & at end, to run async:
$output = shell_exec("nohup php yii <console controller>/<action> > /dev/null &");

是的,我知道应非常谨慎地使用shell_exec,谢谢。

答案 5 :(得分:0)

我认为这是最简单的解决方案:

$controller = new SuggestionController(Yii::$app->controller->id, Yii::$app);
$controller->actionSuggestions();

答案 6 :(得分:0)

通过Web或api

yii2调用控制台命令。

我的控制器名称:

UnitController

在文件夹中

控制台/控制器

我的动作名称:

actionUnitDesc

    if(!defined('STDIN'))  define('STDIN',  fopen('php://stdin',  'rb'));
    if(!defined('STDOUT')) define('STDOUT', fopen('php://stdout', 'wb'));
    if(!defined('STDERR')) define('STDERR', fopen('php://stderr', 'wb'));
    $consoleController = new \console\controllers\UnitController('unit', Yii::$app);
    $consoleController->runAction('unit-des');
相关问题