[英]yii2 how to run console controller function
我被困住了。 我正在嘗試從命令 shell 運行一些 function。 我正在使用基本項目中的 HelloController。 When I run php yii hello
it's working good and the index function is running but if I try to run different function like php yii hello/create
I'm getting this error -
錯誤:未知命令。
我將創建 function 添加到此 controller。 奇怪的是,當我運行php yii
時,我看到了創建命令。 我的 controller 代碼
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
您所呼叫actionIndex
的HelloController
。 此控制器沒有任何其他操作,因此您會看到該錯誤。
干凈的基本應用程序控制台安裝中唯一可用的create
詞位於migrate
部分,因此您可以通過運行在MigrateController
調用actionCreate
php yii migrate/create
所以除非你有一些自定義的控制器/動作,否則沒有其他選擇。
對於所有可用的操作,像以前一樣運行php yii
。 你可以跑
php yii help <command-name>
有關所選命令的幫助。
在指南中閱讀有關控制台命令的更多信息。
按照以下步驟操作后即可使用
當你從主機調用控制台控制器時,你需要知道:PHP 和 YII 的路徑 例如: /usr/bin/php -q /home1/globustm/public_html/yii hello/index
在 localhost(本地 PC)中,您不需要顯示 PHP 和 YII 路徑,因為它默認插入到 SYSTEM PATH 中。
如果你想發送一些參數,你需要在操作后添加它們:例如一個參數: hello/index 'param1'
例如,如果你有多個參數,那么用空格添加它們: hello/index 'param1' 'param2' 'param3'
將您的 controller 放入文件夾 commands/ 順便說一句,有 HelloController.php。 以此類推,制作自己的controller。 意識到:
namespace app\commands;
class MyController extends yii\console\Controller{}
跑:
php yii 我的/動作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.