I am new to the codeigniter 4.0 framework and been trying to upgrade my old code from 3.0 to 4.0 and in the meantime learn the new framework. Currently I am running into an issue which I cannot explain is quite frustrating.
I’ve got a Controller called Account.php located in the AppControllers directory.
<?php
namespace AppControllers;
use AppModelsAccountModel;
class Account extends BaseController
{
private $accountModel;
private $validation;
public function __construct()
{
// Load the Account_model globally in the constructor
$this->accountModel = new AccountModel();
$this->validation = ConfigServices::validation();
}
public function signup_process()
{
// Set validation rules
$this->validation->setRules([
'username' => 'required|max_length[50]',
'email' => 'required|valid_email|max_length[250]',
'firstname' => 'required|max_length[50]',
'lastname' => 'required|max_length[50]',
'birthday' => 'required|valid_date',
'discord' => 'max_length[50]',
]);
// Validation failed, redirect back to the form with validation errors
if (!$this->validation->withRequest($this->request)->run()) {
return redirect()->back()->withInput()->with('errors', $this->validation->getErrors());
}
// If validation passes, proceed with form processing
else
{
// Access form data
$request = service('request');
$arrUser['username'] = $request->getPost('username');
$arrUser['email'] = $request->getPost('email');
$arrUser['firstname'] = $request->getPost('firstname');
$arrUser['lastname'] = $request->getPost('lastname');
$arrUser['birthday'] = $request->getPost('birthday');
$arrUser['discord'] = $request->getPost('discord');
// Insert user data into the database using the model
$this->accountModel->insert_user($arrUser);
return redirect()->to('/account/signup-done');
}
}
}
And I am trying to call a function in my AccountModel.php file located in the AppModels directory
<?php
namespace AppModels;
use CodeIgniterModel;
class AccountModel extends Model
{
public function insert_user($arrUser)
{
print_r($arrUser)
}
}
As soon as I turn on the code $this->accountModel = new AccountModel();, I am greeted with the default Codeigniter error message
Whoops!
We seem to have hit a snag. Please try again later…
I’ve tried looking through the documentation of the framework, but can’t find a clear explanation what might be going wrong.
Stichting Dalaria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.