yii2 如何 运行 console controller 函数
yii2 how to run console controller function
我卡住了。我正在尝试 运行 命令 shell 中的某些功能。
我正在使用基本项目中的 HelloController。
当我 运行 php yii hello
它工作正常并且索引函数是 运行ning 但是如果我尝试 运行 不同的函数,比如 php yii hello/create
我收到这个错误-
Error: Unknown command.
我给这个控制器添加了创建函数。
奇怪的是,当我 运行 php yii
时,我看到了创建命令。
我的控制器代码
namespace app\commands;
use yii\console\Controller;
use Yii;
class HelloController extends Controller
{
public function actionIndex($message = 'hello world')
{
echo $message . "\n";
}
public function actionCreate($message = 'hello world')
{
echo $message . "\n";
}
}
更新:
我的配置文件是
Yii::setAlias('@tests', dirname(__DIR__) . '/tests');
$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');
return [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log', 'gii'],
'controllerNamespace' => 'app\commands',
'modules' => [
'gii' => 'yii\gii\Module',
],
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@app/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => '...',
'username' => '...',
'password' => '...',
'port' => '...',
//'encryption' => 'tls',
],
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
],
'params' => $params,
];
有谁知道如何解决这个问题?
谢谢。
当你运行
php yii hello
您正在呼叫 HelloController
的 actionIndex
。此控制器没有任何其他操作,这就是您看到错误的原因。
干净的基本应用程序控制台安装中唯一可用的 create
字在 migrate
部分,因此您可以通过 运行 在 MigrateController
中调用 actionCreate
宁
php yii migrate/create
因此,除非您有一些自定义 controllers/actions,否则没有其他选择。
对于所有可用的操作 运行 php yii
就像您之前所做的那样。你可以 运行
php yii help <command-name>
有关所选命令的帮助。
阅读 the Guide 中有关控制台命令的更多信息。
执行此步骤后有效
- 转到项目的根文件夹
- 在终端中打开根文件夹路径。
- 现在我的控制器名称是:ContactMigrationController
函数名称是:actionGetContact
- 然后运行 这个cmd php yii contact-migration/get-contact
- 完成。
当您从主机调用控制台控制器时,您需要知道路径:PHP 和 YII
例如:/usr/bin/php -q /home1/globustm/public_html/yii hello/index
在 localhost(本地 PC)中你不需要显示 PHP 和 YII 路径,因为它默认插入到 SYSTEM PATH。
如果你想发送一些参数,你需要在操作后添加它们:
例如一个参数:hello/index 'param1'
例如,如果您有多个参数,则添加它们 space:hello/index 'param1' 'param2' 'param3'
我卡住了。我正在尝试 运行 命令 shell 中的某些功能。
我正在使用基本项目中的 HelloController。
当我 运行 php yii hello
它工作正常并且索引函数是 运行ning 但是如果我尝试 运行 不同的函数,比如 php yii hello/create
我收到这个错误-
Error: Unknown command.
我给这个控制器添加了创建函数。
奇怪的是,当我 运行 php yii
时,我看到了创建命令。
我的控制器代码
namespace app\commands;
use yii\console\Controller;
use Yii;
class HelloController extends Controller
{
public function actionIndex($message = 'hello world')
{
echo $message . "\n";
}
public function actionCreate($message = 'hello world')
{
echo $message . "\n";
}
}
更新: 我的配置文件是
Yii::setAlias('@tests', dirname(__DIR__) . '/tests');
$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');
return [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log', 'gii'],
'controllerNamespace' => 'app\commands',
'modules' => [
'gii' => 'yii\gii\Module',
],
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@app/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => '...',
'username' => '...',
'password' => '...',
'port' => '...',
//'encryption' => 'tls',
],
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
],
'params' => $params,
];
有谁知道如何解决这个问题? 谢谢。
当你运行
php yii hello
您正在呼叫 HelloController
的 actionIndex
。此控制器没有任何其他操作,这就是您看到错误的原因。
干净的基本应用程序控制台安装中唯一可用的 create
字在 migrate
部分,因此您可以通过 运行 在 MigrateController
中调用 actionCreate
宁
php yii migrate/create
因此,除非您有一些自定义 controllers/actions,否则没有其他选择。
对于所有可用的操作 运行 php yii
就像您之前所做的那样。你可以 运行
php yii help <command-name>
有关所选命令的帮助。
阅读 the Guide 中有关控制台命令的更多信息。
执行此步骤后有效
- 转到项目的根文件夹
- 在终端中打开根文件夹路径。
- 现在我的控制器名称是:ContactMigrationController
函数名称是:actionGetContact - 然后运行 这个cmd php yii contact-migration/get-contact
- 完成。
当您从主机调用控制台控制器时,您需要知道路径:PHP 和 YII
例如:/usr/bin/php -q /home1/globustm/public_html/yii hello/index
在 localhost(本地 PC)中你不需要显示 PHP 和 YII 路径,因为它默认插入到 SYSTEM PATH。
如果你想发送一些参数,你需要在操作后添加它们:
例如一个参数:hello/index 'param1'
例如,如果您有多个参数,则添加它们 space:hello/index 'param1' 'param2' 'param3'