【问题标题】:Class 'App\Model\Users' not found in Codeigniter4在 Codeigniter4 中找不到类“App\Model\Users”
【发布时间】:2020-03-07 04:28:06
【问题描述】:

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

找不到类“App\Model\Users”

控制器

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

【问题讨论】:

  • 在你的用户模型中使用namespace App\Models
  • 请分享更多细节。这些文件存储在哪里?

标签: php codeigniter codeigniter-4


【解决方案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()');

}
}

【讨论】:

  • 您的解决方案适用于 Codeigniter 3 而不是 Codeigniter 4
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2020-02-02
    • 2017-12-06
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    相关资源
    最近更新 更多