Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii2 how to run console controller function

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.

like image 393
eli Avatar asked Dec 18 '22 10:12

eli


1 Answers

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.

like image 153
Bizley Avatar answered Jan 02 '23 11:01

Bizley