E
E
EvgMul2016-08-12 12:06:03
Yii
EvgMul, 2016-08-12 12:06:03

How to solve "Class ... not found" error in Yii2?

Hello. I have the following problem.
When accessing the controller action, the error "Class 'app\models\customer\CustomerRecord' not found" is thrown.
The contents of the index.php file

<?php
ini_set('display_errors', true);
define('YII_DEBUG', true);
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');
(new yii\web\Application($config))->run();

Contents of the CustomerRecord.php file
<?php
namespace app\models\customer;

use yii\db\ActiveRecord;

class CustomerRecord extends ActiveRecord
{
  public static function tableName()
  {
    return 'customer';
  }

  public function rules()
  {
    return [
      ['id', 'number'],
      ['name', 'string', 'max' => '256'],
      ['birth_date', 'date', 'format' => 'Y-m-d'],
      ['notes', 'safe']
    ];
  }
}

Well, the contents of the file CustomerController.php
<?php
namespace app\controllers;

use app\models\customer\Customer;
use app\models\customer\CustomerRecord;
use app\models\customer\Phone;
use app\models\customer\PhoneRecord;
use yii\web\Controller;

class CustomerController extends Controller
{
  public function actionIndex()
  {
    $record = $this->findRecordsByQuery();
    return $this->render('index', compact('record'));
  }

  public function actionAdd()
  {
    $customer = new CustomerRecord;
    $phone = new PhoneRecord;
    return $this->render('add', compact('customer', 'phone'));
  }
}

I study according to the book by M. Safronov. - Development of web applications in Yii2.
It seems that he did everything according to the book, but apparently he missed something somewhere.
I can assume that the autoloader does not work, but where to fix it. In general, it will help to understand.
Thanks in advance to all who respond.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2016-08-12
@EvgMul

Your model namespace is app\models\customer, and the file, as you say, is in app\models. Fix it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question