简体   繁体   中英

Class 'App\Model\Users' not found in Codeigniter4

I am working with the latest version of codeigniter framework. Something is wrong with my code, it gives me an error like:

Class 'App\Model\Users' not found

Controller

Filename: 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

File name: 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';
}

If you haven't change your directory names in app, you need to change namespaces from App\Model\Users (without "s" at the end) to App\Model\User.

Namespaces should follow directory structure, unless you change (or extends) CI4's core classes or at least app/Config/Autoload.php

Extends your model with CI_Model for it will be recognized.

 class Auth_model extends CI_Model {

 //you can always put function construct

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

 }
}

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

}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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