代码之家  ›  专栏  ›  技术社区  ›  Arsya

Codeigniter 4上未定义的属性“$M_Auth”.intelephense(1014)

  •  0
  • Arsya  · 技术社区  · 1 年前

    我要制作登录页面,但它一直告诉我 Undefined property '$M_Auth' 当我试图定义我的模型时,它在我的上一个项目中运行良好,但现在当我打开最后一个项目时,也会出现此消息。

    这是我的身份验证控制器

    <?php
    
    namespace App\Controllers;
    
    use App\Controllers\BaseController;
    use App\Models\M_Auth;
    
    class Auth extends BaseController
    {
        public function __construct()
        {
            $this->M_Auth = new M_Auth();
        }
        public function index(){
            echo form_open();
            return view('Auth/login');
        }
    
        public function cek_login(){
            if($this->validate([
                'username' => [
                    'label' => 'Nama Pengguna',
                    'rules' => 'required',
                    'errors' =>[
                        'required' => '{field} tidak boleh kosong!'
                    ]
                ],
                'password' => [
                    'label' => 'Kata Sandi',
                    'rules' => 'required',
                    'errors' =>[
                        'required' => '{field} tidak boleh kosong!'
                    ]
                ] 
            ])){
                //jika valid
                $username = $this->request->getPost('username');
                $password = $this->request->getPost('password');
                $cek = $this->M_Auth->login($username, $password);
            }else{
    
            }
        }
    
    }
    

    这是M_Auth模型

    <?php
    
    namespace App\Models;
    
    use CodeIgniter\Model;
    
    class M_Auth extends Model
    {
        protected $DBGroup          = 'default';
        protected $table            = 'pengguna';
        protected $primaryKey       = 'id_pengguna';
        protected $useAutoIncrement = true;
        protected $allowedFields    = ['level_id', 'username', 'password'];
    
        public function login($username, $password){
            return $this->db->table('pengguna')->where([
                'username' => $username,
                'password' => $password
            ])->get()->getRowArray();
        }
    
    }
    

    我知道出了什么问题,试图通过观看ytb上的教程来修复它,但它与我的代码没有任何不同,并且已经在这里搜索了,但我真的不理解这个问题

    1 回复  |  直到 1 年前
        1
  •  0
  •   Mpmp    1 年前
    ...
    class Auth extends BaseController
    {
        private M_Auth $M_Auth;
        ^^^^^^^^^^^^^^^^^^^^^^
        public function __construct()
        {
            $this->M_Auth = new M_Auth();
        }
        public function index(){
            echo form_open();
            return view('Auth/login');
        }
       ...
    
    推荐文章