在Codeigniter4中找不到类“ App \ Model \ Users”

时间:2019-11-11 13:16:32

标签: php codeigniter codeigniter-4

我正在使用最新版本的Codeigniter框架。我的代码出了点问题,它给我一个错误,例如:

  

未找到“应用\模型\用户”类

控制器

文件名:Auth.php

<?php

namespace App\Controllers;

use CodeIgniter\RESTful\ResourceController;
use App\Model\Users as CodeIgniterUsers;

class Auth extends ResourceController
{
    public function login()
    {
        $model = new CodeIgniterUsers();
        var_dump($model);
    }

    public function register()
    { }
}

模型

文件名:Users.php

<?php

namespace App\Model;

use CodeIgniter\Model;

class Users extends Model
{
    protected $db;
    protected $table = 'user';
    protected $returnType = 'array';

    protected $allowedFields = ['name', 'email', 'password'];
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';
}

2 个答案:

答案 0 :(得分:1)

使用CI_Model扩展您的模型,因为它将被识别。

 class Auth_model extends CI_Model {

 //you can always put function construct

   public function __construct (){
  parent::__construct ();

 }
}

在控制器中:

 class User extends CI_Controller {
 public function __construct () {
 parent:: __construct();
 //you can load here the model that you will just often so will load it everytime to use it in a function

 $this->load->auth_model('model-name(exam- public function test_uer()');

}
}

答案 1 :(得分:0)

如果尚未在app中更改目录名称,则需要将名称空间从App \ Model \ User(末尾没有“ s”)更改为App \ Models \ User。

除非您更改(或扩展)CI4的核心类或至少更改app / Config / Autoload.php,否则命名空间应遵循目录结构