php - 我无法弄清楚为什么我的 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 规则数组中的密码验证行才能使其正常工作吗?
谢谢你的帮助。
解决方案
推荐阅读
- google-colaboratory - Google Colab 会话可以访问另一个会话的虚拟机中的文件吗?
- docker - 如何修复 microk8s 上的“无法拉取图像”
- sql - 在 where 子句中具有相关子查询的 SQL 查询
- node.js - Electron:使用 showOpenDialog 允许在 Linux 和 Win10 上选择文件,但为什么我不能在 MacOS 上选择文件?
- reactjs - 如何将 Next.js 动态与导出多个函数的库一起使用?
- mysql - 如何提高 vtigercrm 机会模块的速度
- javascript - 在比较 jwt.iat(发布于)属性与当前日期时间时遇到问题 - (“自纪元以来的秒数”NumericDate)
- javascript - Javascript reportvalidity() 每隔一段时间才有效
- python - 升级 Django 和 Python 版本,将 FloatField 的默认值设置为字符串的问题
- reactjs - GitHub Page 主页子域抛出 404