yii2 how to run console controller function

25.7k views Asked by At

I'm stuck. I'm trying to run some function from the command shell. I'm using the HelloController from the basic project. 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. The strange thing is that when I run php yii I'm seeing the create command. My controller code

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.

4

There are 4 answers

10
Bizley On BEST ANSWER

When you run

php yii hello

you are calling actionIndex of HelloController. 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

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. You can run

php yii help <command-name>

for help about selected command.

Read more about console commands in the Guide.

0
Murat Kurbanov On

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

In localhost (local PC) you don't need show PHP and YII paths, because its inserted to SYSTEM PATH by default.

If you want send some parameters you need add them after action: For example one parameter: hello/index 'param1'

For example if you have more than one parameter, so add them with space: hello/index 'param1' 'param2' 'param3'

0
Konstantin Konstantynin On

put your controller in the folder commands/ By the way, there is HelloController.php. Make your own controller by analogy. Be aware:

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

run:

php yii my/action

0
Krishna Jangid On

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
    and function name is : actionGetContact
  4. then run this cmd php yii contact-migration/get-contact
  5. done.