Class 'app\controllers\villages' not found

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

    1. 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.

You didn’t added location for Viilages model in SiteController add use app\models\Villages; in SiteController.

1 Like

Thank you for your reply.

I Added the Line and now im getting another error:
# Unknown Class – [yii\base\UnknownClassException](http://www.yiiframework.com/doc-2.0/yii-base-unknownclassexception.html)

## Unable to find 'app\models\Villages' in file: C:\Users\username\Documents\xampp_php7\htdocs\rootDirectory/models/Villages.php. Namespace missing?

Namespace should be app\models in Villages Model. Change namespace from namespace app\models\Villages to namespace app\models in Villages Model.

1 Like

This works!
I will deal with yii and namespaces again. Thank you so much!