问题描述
我刚刚安装了yii2高级模板,创建了一个News模型,现在我想创建Crud(带有gii),但是当我单击预览时出现此错误。
我没有更改高级模板中的其他内容。
我正在使用wamp
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.
I did not change anything else in the advanced template.
I'm using 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();
推荐答案
我刚刚遇到了相同的错误消息。在我的情况下,问题是通过Yii :: t打开I18n时。事实证明,Gii在生成模型模板(即公共函数attributeLabels()
方法)时不以某种方式添加前导 \。我更改了
I have just encountered the same error message. In my case the problem was when turning on I18n by means of Yii::t. It turned out that Gii somehow does not add leading "\" when generating a model template, namely public function attributeLabels()
method. I changed
return [
'id' => \Yii::t('app', 'ID'),
'fullName' => \Yii::t('app', 'Full Name'),
];
至
return [
'id' => \Yii::t('app', 'ID'),
'fullName' => \Yii::t('app', 'Full Name'),
];
,错误消失了。尝试做同样的事情。
and the error disappeared. Try to do the same.
这篇关于未找到Yii2 GII Crud错误类别'app\models\Yii'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!