Hello,
I created a model with Gii and am now trying to work with the model. Unfortunately I always get the following error message:
Error
Class ‘yii\web\models\Villages’ not found
- in rootDirectory\controllers\SiteController.php
This is my SiteController:
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
class SiteController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout', 'index'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* {@inheritdoc}
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex()
{
$Villages = new Villages(); # The Error happens on this line
return $this->render('index.php');
}
/**
Zeigt alle Dörfer an
**/
public function actionAllvillages()
{
return $this->render('index.php');
}
}
And this is my Model:
<?php
namespace app\models\Villages;
use Yii;
class Villages extends \yii\db\ActiveRecord
{
...
}
I have already looked at other questions, so I suspect that there is an error with the namespace. The way I see it, the namespaces are correct.
The Rootdirectory looks like this:
.idea
assets
commands
config
controllers (Contails SiteController.php)
mail
models (Contains Villages.php)
runtime
tests
themes
vagrant
vendor
views (Contains the view site/index.php)
web
widgets
do you have any ideas?
Solution:
added use app\models\Villages;
in SiteController
replaced namespace app\models\Villages
with app\models
in Villages Model.
Thanks to InsaneSkull.