未捕获的错误异常:strtolower():不赞成将null传递给字符串类型的参数#1($string)



我正在运行codeigniter 4.1.5和php 8.1.0,当我尝试输入数据时,这就是我得到的错误

未捕获的错误异常:strtolower((:在C:\examplep\htdocs\cikaryawan\system\Validation\FormatRules.php:253中,不赞成将null传递给字符串类型的参数#1($string(堆栈跟踪:

  1. #0[内部函数]:CodeIgniter\Debug\Exceptions->errorHandler((
  2. #1 C:\examplep\htdocs\cikaryawan\system\Validation\FormatRules.php(253(:strtolower((
  3. #2 C:\examplep\htdocs\cikaryawan\system\HTTP\RequestTrait.php(151(:CodeIgniter\Validation\FormatRules->valid_ip((
  4. #3 C:\examplep\htdocs\cikaryawan\app\Views\errors\html\error_exception.php(206(:CodeIgniter\HTTP\Request->getIPAddress((
  5. #4 C:\examplep\htdocs\cikaryawan\system\Debug\Exceptions.php(229(:include('…'(
  6. #5 C:\examplep\htdocs\cikaryawan\system\Debug\Exceptions.php(232(:CodeIgniter\Debug\Axceptions->CodeIgniter\Debug{closure}((
  7. #6 C:\examplep\htdocs\cikaryawan\system\Debug\Exceptions.php(116(:CodeIgniter\Debug\Axceptions->render((
  8. #7[内部函数]:CodeIgniter\Debug\Exceptions->exceptionHandler((
  9. #8{main}在我的.env文件夹中
CI_ENVIRONMENT = development
database.default.hostname = localhost
database.default.database = cikaryawanauth
database.default.username = root
database.default.password = 
database.default.DBDriver = MySQLi
database.default.DBPrefix =

这是我的register.php

<body>
<div class="container">
<div class="row" style="margin-top:45px">
<div class="col-md-4 col-md-offset-4">
<h4>Sign Up</h4>
<hr>
<form action="<?= base_url('auth/save') ?>" method="post">
<?= csrf_field(); ?>
<div class="form-group">
<label for="">Name</label>
<input type="text" class="form-control" name="name" placeholder="Enter your name" value="<?= set_value('name'); ?>">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'name') : '' ?></span>
</div>
<div class="form-group">
<label for="">Email</label>
<input type="email" class="form-control" name="email" placeholder="Enter your email" value="<?= set_value('email'); ?>">
<span class=" text-danger"><?= isset($validation) ? display_error($validation, 'email') : '' ?></span>
</div>
<div class="form-group">
<label for="">Password</label>
<input type="password" class="form-control" name="password" placeholder="Enter password">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'password') : '' ?></span>
</div>
<div class="form-group">
<label for="">Confirm Password</label>
<input type="password" class="form-control" name="cpassword" placeholder="Confirm password">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'cpassword') : '' ?></span>
</div>
<div class="form-group">
<button class="btn btn-primary btn-block" type="submit">Sign Up</button>
</div>
<a href="<?= site_url('auth/') ?>">I already have account, login now</a>
</form>
</div>
</div>
</div>
</body>

这是我作为控制器的auth.php

<?php
namespace AppControllers;
class Auth extends BaseController
{
public function __construct()
{
helper(['url', 'form']);
}
public function index()
{
return view('auth/login');
}
public function register()
{
return view('auth/register');
}
public function save()
{
$validation = $this->validate([
'name' => [
'rules' => 'required',
'errors' => [
'required' => 'Your name is required'
]
],
'email' => [
'rules' => 'required|valid_emails|is_unique[users.email]',
'errors' => [
'required' => 'Email is required',
'valid_email' => 'You must enter a valid email',
'is_unique' => 'Email already taken'
]
],
'password' => [
'rules' => 'required|min_length[5]|max_length[12]',
'errors' => [
'required' => 'Password is required',
'min_lenght' => 'Password must have atleast 5 character',
'max_lenght' => 'Password must not exceed 12 character'
]
],
'cpassword' => [
'rules' => 'required|min_length[5]|max_length[12]|matches[password]',
'errors' => [
'required' => 'Password is required',
'min_lenght' => 'Password must have atleast 5 character',
'max_lenght' => 'Password must not exceed 12 character',
'matches' => 'Confirms password not matches to password'
]
]
]);
if (!$validation) {
return view('auth/register', ['validation' => $this->validator]);
} else {
$name = $this->request->getPost('name');
$email = $this->request->getPost('email');
$password = $this->request->getPost('password');
$values = [
'name' => $name,
'email' => $email,
'password' => $password,
];
$userModels = new AppModelsUsersModel();
$query = $userModels->insert($values);
if (!$query) {
return redirect()->back()->with('fail', 'something went wrong');
} else {
return redirect()->to('register')->with('success', 'You are now registered');
}
}
}
}

这是我在helpers文件夹中的form_helper.php中放入的内容

<?php
function display_error($validation, $field)
{
if ($validation->hasError($field)) {
return $validation->getError($field);
} else {
return false;
}
}

我做错了什么?

您可以将空合并运算符添加到这行代码中:

$method = strtolower($method);

替换为:

$method = strtolower($method ?? '');

应在编号为835 的library/RestController文件行中进行此更改

CodeIgniter 4.1.5还不支持PHP 8.1。请使用8.0

或使用CI4 的develop分支

https://github.com/codeigniter4/CodeIgniter4/pull/4883

https://github.com/codeigniter4/CodeIgniter4/issues/5436

https://forum.codeigniter.com/thread-80490-post-391352.html#pid391352

https://forum.codeigniter.com/thread-80413.html

最新更新