Recently, I started a new project and installed a new Ubuntu machine with default installed PHP environment which was the version of 8.1. Considering future use, I take a few days to upgrade the codeigniter3 framework.
During the upgrade process, many parts were found that were not mentioned in the documentation, I recorded it for your reference
All php class,remove the first line of if statement which checking if the BASEPATH constant exists.This statement is no need anymore because your website root dir moved to /public,all php file are not allowed to be direct access now.
//codeigniter3 <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
//codeigniter4 <?php namespace App\Models;
The series of classes starting with 'CI_' no longer exist, no need to inherit.
//codeigniter3 class Role_model extends CI_Model
//codeigniter4 namespace App\Models; class Role_model
Using 'config' function to load config class.
//codeigniter3 $CI =&get_instance(); if ($CI->config->load('redis', TRUE, TRUE)) { $redisConfig = $CI->config->item('redis'); }else{ log_message('error', 'Cache: Redis config empty.'); }
//codeigniter4 $config = config('Cache'); $redisConfig = $config->redis;
A sample of using redis.
//codeigniter3 $this->load->driver('cache'); $ret = $this->cache->redis->save(self::RDS_KEY,$user_id, self::RDS_TIMEOUT);
//codeigniter4 //app/Config/Cache.php public string $handler = 'redis'; $cache = \Config\Services::cache(); $ret = $cache->get(self::RDS_KEY,$user_id, self::RDS_TIMEOUT);
Load all model by using model function,and provide the namespace.
//codeigniter3 $this->load->model('Customer_model'); $ret = $this->Customer_model->get($data['username'],'username');
//codeigniter4 $customerModel = model('App\Models\Customer_model'); $ret = $customerModel->get($data['username'],'username');
Still want to use "$this->your_Model"? The solution with minimal code changes:
class Menu extends AdminController { protected $Base_model; protected $permission; function __construct() { parent::__construct(); $this->Base_model = model('App\Models\System_Base_model'); $this->permission = model('App\Libraries\Permission'); }
DB Query.
class BaseModel { protected $table = ''; protected $conn = ''; protected $db = ''; public function __construct() { /** * @var BaseConnection|null $db */ $this->conn = \Config\Database::connect(); $this->db = $this->conn->table($this->table); }
$this->db->query('SELECT FOUND_ROWS() AS `Count`'); $this->conn->query('SELECT FOUND_ROWS() AS `Count`'); $this->db->insert_id(); $this->conn->insertID(); $this->db->affected_rows(); $this->conn->affectedRows(); $query->result_array(); $query->getResultArray(); $this->db->count_all_results(); $this->db->countAllResults(); $this->db->where_in($field, $id); $this->db->whereIn($field, $id)
The above code does not cover all the changes, and only gives a brief reminder of the general modification. Search and find all the feature codes and modify them directly, the migration efficiency will be greatly improved.