简体   繁体   English

Yii2 gii crud 错误 Class 'app\models\Yii' not found

[英]Yii2 gii crud error Class 'app\models\Yii' not found

I just installed yii2 advanced template, I created a model News and now I want to create the Crud (with gii), but when I click 'preview' I get this error.我刚刚安装了 yii2 高级模板,我创建了一个 model 新闻,现在我想创建 Crud(使用 gii),但是当我单击“预览”时出现此错误。
I did not change anything else in the advanced template.我没有更改高级模板中的任何其他内容。
I'm using wamp我正在使用 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();

I have just encountered the same error message. 我刚刚遇到了同样的错误消息。 In my case the problem was when turning on I18n by means of Yii::t. 在我的情况下,问题是当通过Yii :: t打开I18n时。 It turned out that Gii somehow does not add leading "\\" when generating a model template, namely public function attributeLabels() method. 事实证明,Gii在生成模型模板时不会添加前导“\\”,即public function attributeLabels()方法。 I changed 我变了

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

to

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

and the error disappeared. 并且错误消失了。 Try to do the same. 尝试做同样的事情。

I had the same issue.我遇到过同样的问题。 You might want to check where your models are placed.您可能想检查模型的放置位置。 Since its an advanced template in this case, changing the namespace from apps/models to backend/models will fix it.由于在这种情况下它是一个高级模板,将命名空间从 apps/models 更改为 backend/models 将修复它。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM