如何在基本模板中从控制器操作中运行命令?
我试过How to run console command in yii2 from web
这是失败的:
public function actionTest(){
$oldApp = \Yii::$app;
$console = new \yii\console\Application([
'id' => 'basic-console',
'basePath' => '@app/commands',
'components' => [
'db' => $oldApp->db,
],
]);
\Yii::$app->runAction('hello/index');
\Yii::$app = $oldApp;
}以上向我展示了Unknown command: hello/index Did you mean "help/index"?
司令部:
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace app\commands;
use yii\console\Controller;
/**
* This command echoes the first argument that you have entered.
*
* This command is provided as an example for you to learn how to create console commands.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class HelloController extends Controller
{
/**
* This command echoes what you have entered as the message.
* @param string $message the message to be echoed.
*/
public function actionIndex($message = 'hello world')
{
echo $message . "\n";
}
}请帮帮我!
发布于 2017-03-10 12:37:02
发生这种情况有两个原因。
'controllerNamespace'设置为'app\commands'。'basePath'的值是错误的。如果您是从SiteController运行这个值,那么这个值应该是__DIR__ . '/../'我建议你做点不同的事。让我们再说一遍,假设您是从SiteController运行的,我会这样做:
$config = require(__DIR__ . '/../config/console.php');
$console = new \yii\console\Application($config);这样,您将使用与运行./yii hello/index时相同的配置
https://stackoverflow.com/questions/42717342
复制相似问题