Yii2 gii crud错误类'app \ models \ Yii'未找到

时间:2014-03-24 09:19:29

标签: php yii crud gii yii2

我刚刚安装了yii2高级模板,我创建了一个模型新闻,现在我想创建Crud(用gii),但是当我点击预览'我收到这个错误。
我没有更改高级模板中的任何其他内容 我正在使用wamp

 PHP Fatal Error – yii\base\ErrorException
Class 'app\models\Yii' not found

    1. in C:\wamp\www\advanced\backend\models\News.php at line 44
    2. in C:\wamp\www\advanced\vendor\yiisoft\yii2-gii\generators\crud\default\search.php – yii\gii\generators\crud\Generator::generateSearchLabels() at line 18
    1314151617181920212223

    $searchModelClass = StringHelper::basename($generator->searchModelClass);
    if ($modelClass === $searchModelClass) {
        $modelAlias = $modelClass . 'Model';
    }
    $rules = $generator->generateSearchRules();
    $labels = $generator->generateSearchLabels();
    $searchAttributes = $generator->getSearchAttributes();
    $searchConditions = $generator->generateSearchConditions();

    echo "<?php\n";
    ?>

    3. in C:\wamp\www\advanced\vendor\yiisoft\yii2\base\View.php – unknown() at line 312
    4. in C:\wamp\www\advanced\vendor\yiisoft\yii2\base\View.php – yii\base\View::renderPhpFile() at line 244
    5. in C:\wamp\www\advanced\vendor\yiisoft\yii2-gii\Generator.php – yii\base\View::renderFile() at line 315
    310311312313314315316317318319320

        public function render($template, $params = [])
        {
            $view = new View;
            $params['generator'] = $this;

            return $view->renderFile($this->getTemplatePath() . '/' . $template, $params, $this);
        }

        /**
         * Validates the template selection.
         * This method validates whether the user selects an existing template

    6. in C:\wamp\www\advanced\vendor\yiisoft\yii2-gii\generators\crud\Generator.php – yii\gii\Generator::render() at line 166
    161162163164165166167168169170171

        {
            $controllerFile = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->controllerClass, '\\')) . '.php');
            $searchModel = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->searchModelClass, '\\') . '.php'));
            $files = [
                new CodeFile($controllerFile, $this->render('controller.php')),
                new CodeFile($searchModel, $this->render('search.php')),
            ];

            $viewPath = $this->getViewPath();
            $templatePath = $this->getTemplatePath() . '/views';
            foreach (scandir($templatePath) as $file) {

    7. in C:\wamp\www\advanced\vendor\yiisoft\yii2-gii\controllers\DefaultController.php – yii\gii\generators\crud\Generator::generate() at line 44
    3940414243444546474849

            $generator = $this->loadGenerator($id);
            $params = ['generator' => $generator, 'id' => $id];
            if (isset($_POST['preview']) || isset($_POST['generate'])) {
                if ($generator->validate()) {
                    $generator->saveStickyAttributes();
                    $files = $generator->generate();
                    if (isset($_POST['generate']) && !empty($_POST['answers'])) {
                        $params['hasError'] = $generator->save($files, (array) $_POST['answers'], $results);
                        $params['results'] = $results;
                    } else {
                        $params['files'] = $files;

    8. in C:\wamp\www\advanced\vendor\yiisoft\yii2\base\InlineAction.php – yii\gii\controllers\DefaultController::actionView() at line 54
    9. in C:\wamp\www\advanced\vendor\yiisoft\yii2\base\InlineAction.php – call_user_func_array() at line 54
    10. in C:\wamp\www\advanced\vendor\yiisoft\yii2\base\Controller.php – yii\base\InlineAction::runWithParams() at line 127
    11. in C:\wamp\www\advanced\vendor\yiisoft\yii2\base\Module.php – yii\base\Controller::runAction() at line 435
    12. in C:\wamp\www\advanced\vendor\yiisoft\yii2\web\Application.php – yii\base\Module::runAction() at line 84
    13. in C:\wamp\www\advanced\vendor\yiisoft\yii2\base\Application.php – yii\web\Application::handleRequest() at line 312
    14. in C:\wamp\www\advanced\backend\web\index.php – yii\base\Application::run() at line 17
    121314151617

        require(__DIR__ . '/../config/main.php'),
        require(__DIR__ . '/../config/main-local.php')
    );

    $application = new yii\web\Application($config);
    $application->run();

1 个答案:

答案 0 :(得分:2)

我刚刚遇到了同样的错误消息。在我的情况下,问题是当通过Yii :: t打开I18n时。事实证明,在生成模型模板时,Gii不会以某种方式添加前导“\”,即public function attributeLabels()方法。我改变了

return [
  'id' => \Yii::t('app', 'ID'),
  'fullName' => \Yii::t('app', 'Full Name'),
];

return [
   'id' => \Yii::t('app', 'ID'),
   'fullName' => \Yii::t('app', 'Full Name'),
];

并且错误消失了。尝试做同样的事。