Yii2 添加新的控制台控制器


Yii2 Adding new console controller

为什么当我尝试使用控制台控制器时yii2抛出异常?法典:

<?php
namespace app'commands;
use yii'console'Controller;
class Hashtag extends Controller
{

    public function actionIndex($search = 'test')
    {
      echo $search;
    }
}

控制器位于:app''command''HashtagController使用终端时:php yii hashtag

Exception 'yii'base'UnknownClassException' with message 'Unable to find 'app'commands'HashtagController' in file: /var/www/html/yiitask/yii2/commands/HashtagController.php. Namespace missing?'
in /var/www/html/yiitask/yii2/vendor/yiisoft/yii2/BaseYii.php:291

此文件夹中之前创建的其他控制器运行良好。

您的命名空间错误 正确设置控制台控制器的命名空间,例如:(取决于您的控制台控制器目录的位置)

  namespace app'console'controllers;

那么可能是缺少控制器

class HashtagController extends Controller
{

而不是

class Hashtag extends Controller
{