3

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.

Bizley
  • 15,937
  • 5
  • 43
  • 53
eli
  • 125
  • 2
  • 3
  • 10

2 Answers2

9

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.

Bizley
  • 15,937
  • 5
  • 43
  • 53
  • i know what you said, i added the create function to this controller and then i tried to run it but still only the index function is running. – eli Jan 02 '17 at 04:30
  • @eli Double check your controller because this code of yours works without errors. I've just checked it. – Bizley Jan 02 '17 at 17:22
  • its keep telling me the error message. is it related to other things? – eli Jan 02 '17 at 17:28
  • how do you call the create function? like this? php yii hello/create – eli Jan 02 '17 at 17:30
  • Yes. It must be something else then. Is it clean Basic App Project? – Bizley Jan 02 '17 at 17:30
  • Yes its the basic app, but I have web controllers that working good. now I tried to add console controller and I got this error. – eli Jan 02 '17 at 17:35
  • Check for simple typos like `craete` instead of `create` and so on. – Bizley Jan 02 '17 at 17:37
  • I checked and its not working for me. even if i run "php yii hello/index" I get this error. its like its cant get the name of the function, do you have any ideas? – eli Jan 04 '17 at 05:32
  • Never give your credentials like that! I've removed them but it is still available in revision details. Consider changing them... Anyway I can not see any problems in your config file. I'm out of ideas how to help you unfortunately. – Bizley Jan 06 '17 at 08:58
  • i forgot... and i change them for sure. thanks for your help – eli Jan 06 '17 at 09:36
1

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.
Krishna Kumar Jangid
  • 2,275
  • 1
  • 15
  • 23