首页 > 解决方案 > 我无法弄清楚为什么我的 UserModel 在 CodeIgniter 4 中出现错误

问题描述

我正在尝试让 Oauth2 与 CodeIgniter 一起使用,但在尝试使用 Postman 发布请求时,我一直遇到 500 错误。

我发现如果我注释掉新 UserModel 的实例化,错误就会停止发生。

用户.php(控制器):

<?php namespace App\Controllers;

use \App\Libraries\Oauth;
use \OAuth2\Request;
use CodeIgniter\API\ResponseTrait;
use App\Models\UserModel;


class User extends BaseController
{
    use ResponseTrait;

    public function login(){
        $oauth = new Oauth();
        $request = new Request();
        $respond = $oauth->server->handleTokenRequest($request->createFromGlobals());
        $code = $respond->getStatusCode();
        $body = $respond->getResponseBody();
        return $this->respond(json_decode($body), $code);
       
    }

    public function register(){
        helper('form');
        $data = [];

        if($this->request->getMethod() != 'post')
            return $this->fail('Only post request is allowed');

        
           
        $validation =  \Config\Services::validation();

        
        
        $rules = array(
            'firstname' => 'required|min_length[3]|max_length[20]',
            'lastname' => 'required|min_length[3]|max_length[20]',
            'email' => 'required|valid_email',
            //'email' => 'is_unique[users.email]',
            'password' => 'required|min_length[8]',
            'password_confirm' => 'matches[password]',
        );
  
        if(!$this->validate($rules)){
            return $this->respond($validation->getErrors());
            
        }else{ 
            
            $model = new UserModel();
            
            $data = [
            'firstname' => $this->request->getVar('firstname'),
            'lastname' => $this->request->getVar('lastname'),
            'email' => $this->request->getVar('email'),
            'password' => $this->request->getVar('password'),
            ];
 
            
            $user_id = $model->insert($data);
            $data['id'] = $user_id;
            unset($data['password']);
            

            return $this->respondCreated($data);
           
        }
    }
}

用户模型.php:

<?php namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model{
    
  protected $table = 'users';
  protected $primaryKey = 'id';
  protected $allowedFields = ['firstname','lastname','password','email'];
  
  protected $beforeInsert = ['beforeInsert'];
  protected $beforeUpdate = ['beforeUpdate'];

  
  protected function beforeInsert(array $data){
    $data = $this->passwordHash($data);
    return $data;
  }

  protected function beforeUpdate(array $data){
    $data = $this->passwordHash($data);
    return $data;
  }


  protected function passwordHash(array $data){
    if(isset($data['data']['password']))
      $data['data']['password'] = password_hash($data['data']['password'], PASSWORD_DEFAULT);

    return $data;
  }

}

我不知道问题是否与 Oauth 相关,但如果是,请告诉我,我可以提供其他代码。

比如 login() 函数中的 Oauth.php:

<?php namespace App\Libraries;

//use \OAuth2\Storage\Pdo;
use \App\Libraries\CustomOauthStorage;


class Oauth{
  var $server;

  function __construct(){
    $this->init();
  }

  public function init(){
    $dsn = getenv('database.default.DSN');
    $username = getenv('database.default.username');
    $password = getenv('database.default.password');

    $storage = new CustomOauthStorage(['dsn' => $dsn, 'username' => $username, 'password' => $password]);
    $this->server = new \OAuth2\Server($storage);
    $this->server->addGrantType(new \OAuth2\GrantType\UserCredentials($storage));
  }
}

在 Postman 中发布时,此代码会产生服务器 500 错误。当我注释掉 UserModel 实例化时,该请求有效。当我谈到这个主题时,有人会碰巧知道为什么我必须注释掉 User.php 规则数组中的密码验证行才能使其正常工作吗?

谢谢你的帮助。

标签: phpcodeigniter-4

解决方案


推荐阅读