繁体   English   中英

在 Codeigniter4 中找不到 Class 'App\Model\Users'

[英]Class 'App\Model\Users' not found in Codeigniter4

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

Class 'App\Model\Users' 未找到

Controller

文件名: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()
    { }
}

Model

文件名: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';
}

如果您还没有更改应用程序中的目录名称,则需要将命名空间从 App\Model\Users(末尾不带“s”)更改为 App\Model\User。

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

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

 class Auth_model extends CI_Model {

 //you can always put function construct

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

 }
}

在 controller 中:

 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()');

}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM