简体   繁体   English

yii2 如何运行控制台 controller function

[英]yii2 how to run console controller function

I'm stuck.我被困住了。 I'm trying to run some function from the command shell.我正在尝试从命令 shell 运行一些 function。 I'm using the HelloController from the basic project.我正在使用基本项目中的 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 - 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 -

Error: Unknown command.错误:未知命令。

I added the create function to this controller.我将创建 function 添加到此 controller。 The strange thing is that when I run php yii I'm seeing the create command.奇怪的是,当我运行php yii时,我看到了创建命令。 My controller code我的 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";
    }

}

UPDATED: My Config file is更新:我的配置文件是

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,
];

Does anyone know how to solve this issue?有谁知道如何解决这个问题? Thanks.谢谢。

When you run当你跑

php yii hello

you are calling actionIndex of HelloController .您所呼叫actionIndexHelloController This controller does not have any other actions so that is why you see the error.此控制器没有任何其他操作,因此您会看到该错误。

The only create word available in clean Basic App console installation is in the migrate section so you can call actionCreate in MigrateController by running干净的基本应用程序控制台安装中唯一可用的create词位于migrate部分,因此您可以通过运行在MigrateController调用actionCreate

php yii migrate/create

So unless you have got some custom controllers/actions there are no other options.所以除非你有一些自定义的控制器/动作,否则没有其他选择。

For all available actions run php yii like you did before.对于所有可用的操作,像以前一样运行php yii You can run你可以跑

php yii help <command-name>

for help about selected command.有关所选命令的帮助。

Read more about console commands inthe Guide .指南中阅读有关控制台命令的更多信息。

it works after follow this steps按照以下步骤操作后即可使用

  1. goto your root folder of project转到您的项目根文件夹
  2. open root folder path in terminal.在终端中打开根文件夹路径。
  3. now my controller name is : ContactMigrationController现在我的控制器名称是: ContactMigrationController
    and function name is : actionGetContact和函数名称是: actionGetContact
  4. then run this cmd php yii contact-migration/get-contact然后运行这个 cmd php yii contact-migration/get-contact
  5. done.完毕。

When you call console controller from hosting, you need to know path of: PHP and YII For example: /usr/bin/php -q /home1/globustm/public_html/yii hello/index当你从主机调用控制台控制器时,你需要知道:PHP 和 YII 的路径 例如: /usr/bin/php -q /home1/globustm/public_html/yii hello/index

In localhost (local PC) you don't need show PHP and YII paths, because its inserted to SYSTEM PATH by default.在 localhost(本地 PC)中,您不需要显示 PHP 和 YII 路径,因为它默认插入到 SYSTEM PATH 中。

If you want send some parameters you need add them after action: For example one parameter: hello/index 'param1'如果你想发送一些参数,你需要在操作后添加它们:例如一个参数: hello/index 'param1'

For example if you have more than one parameter, so add them with space: hello/index 'param1' 'param2' 'param3'例如,如果你有多个参数,那么用空格添加它们: hello/index 'param1' 'param2' 'param3'

put your controller in the folder commands/ By the way, there is HelloController.php.将您的 controller 放入文件夹 commands/ 顺便说一句,有 HelloController.php。 Make your own controller by analogy.以此类推,制作自己的controller。 Be aware:意识到:

namespace app\commands;
class MyController extends yii\console\Controller{}

run:跑:

php yii my/action php yii 我的/动作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM