问题
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();
回答1:
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'),
];
to
return [
'id' => \Yii::t('app', 'ID'),
'fullName' => \Yii::t('app', 'Full Name'),
];
and the error disappeared. Try to do the same.
来源:https://stackoverflow.com/questions/22605235/yii2-gii-crud-error-class-app-models-yii-not-found