| name | codeigniter |
| description | Use when working with CodeIgniter 3 or CodeIgniter 4 applications — controllers, models, views, routing, database queries, migrations, libraries, helpers, security, file uploads, email, caching, testing, services, events, CLI commands, and deployment. Trigger on: CI3 projects with system/core/CodeIgniter.php or index.php bootstrapping CI, CI4 projects with app/Config/App.php or spark CLI, any mention of CodeIgniter, HMVC in CI3, Shield auth in CI4, CI query builder usage, or 'php spark' commands. |
| license | MIT |
| metadata | {"author":"yasserstudio","version":"1.0.0"} |
CodeIgniter Development
You are a CodeIgniter expert. You work with both CI3 (3.1.x) and CI4 (4.x, latest stable 4.7.x) and understand their fundamental architectural differences. Always identify which version the project uses before writing any code.
For deep dives on version-specific features, see:
- CI3 deep dive — file uploads, email, caching, pagination, libraries, HMVC, hooks, helpers, logging
- CI4 deep dive — REST controllers, entities, filters, migrations, seeders, uploads, email, caching, services, events, views, cells, Spark CLI, testing, Shield auth, content negotiation
- Deployment & migration — server config, production checklist, full CI3→CI4 migration table
Version Detection
| Signal | Version |
|---|
system/core/CodeIgniter.php exists | CI3 |
$this->load->model() / $this->load->view() | CI3 |
application/config/config.php | CI3 |
app/Config/App.php exists | CI4 |
namespace App\Controllers; in controllers | CI4 |
spark CLI at project root | CI4 |
composer.json requires codeigniter4/framework | CI4 |
CI3 — Core Patterns
Project Structure
project/
├── application/
│ ├── config/ # autoload.php, config.php, database.php, routes.php, hooks.php
│ ├── controllers/
│ ├── models/
│ ├── views/
│ ├── libraries/
│ ├── helpers/
│ ├── hooks/
│ ├── core/ # MY_Controller, MY_Model overrides
│ └── third_party/
├── system/ # framework core — never edit
└── index.php # front controller
Controllers
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Products extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('product_model');
$this->load->helper('url');
}
public function index()
{
$data['products'] = $this->product_model->get_all();
$this->load->view('products/index', $data);
}
public function view($id)
{
$data['product'] = $this->product_model->get($id);
if (empty($data['product'])) {
show_404();
}
$this->load->view('products/view', $data);
}
}
Rules: Class name MUST match filename (first letter uppercase). Always call parent::__construct(). On Linux, filenames are case-sensitive.
Models
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Product_model extends CI_Model
{
protected $table = 'products';
public function get_all()
{
return $this->db->get($this->table)->result();
}
public function get($id)
{
return $this->db->get_where($this->table, ['id' => $id])->row();
}
public function insert($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
public function update($id, $data)
{
$this->db->where('id', $id);
return $this->db->update($this->table, $data);
}
public function delete($id)
{
return $this->db->delete($this->table, ['id' => $id]);
}
}
Query Builder
$results = $this->db
->select('p.*, c.name as category_name')
->from('products p')
->join('categories c', 'c.id = p.category_id', 'left')
->where('p.active', 1)
->where('p.price >', 10)
->order_by('p.name', 'ASC')
->limit(20, $offset)
->get()
->result();
$this->db->insert('products', ['name' => $name, 'price' => $price]);
$this->db->where('id', $id)->update('products', ['price' => $new_price]);
$this->db->where('id', $id)->delete('products');
$this->db->trans_start();
$this->db->insert('orders', $order_data);
$this->db->insert('order_items', $item_data);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) { }
$this->db->query('SELECT * FROM products WHERE slug = ?', [$slug]);
Routing
$route['default_controller'] = 'home';
$route['404_override'] = 'errors/page_missing';
$route['products'] = 'catalog/index';
$route['products/(:num)'] = 'catalog/view/$1';
$route['api/products/(:any)'] = 'api/products/$1';
Input & Security
$name = $this->input->post('name', TRUE);
$id = $this->input->get('id');
$config['csrf_protection'] = TRUE;
<?php echo html_escape($user_input); ?>
Form Validation
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required|min_length[3]|max_length[255]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
if ($this->form_validation->run() === FALSE) {
$this->load->view('form', $data);
} else {
$this->product_model->insert($this->input->post());
redirect('products');
}
Sessions
$this->session->set_userdata('user_id', $id);
$user_id = $this->session->userdata('user_id');
$this->session->set_flashdata('success', 'Product created.');
CI4 — Core Patterns
Project Structure
project/
├── app/
│ ├── Config/ # App.php, Database.php, Routes.php, Filters.php, Services.php, Events.php
│ ├── Controllers/ # BaseController.php
│ ├── Models/
│ ├── Views/
│ ├── Filters/
│ ├── Entities/
│ ├── Cells/
│ ├── Commands/
│ ├── Database/ # Migrations/, Seeds/
│ └── Language/
├── public/ # web root — point server here
│ └── index.php
├── writable/ # cache, logs, session — must be writable by web server
├── tests/
├── .env
├── spark # CLI tool
└── composer.json
Controllers
<?php
namespace App\Controllers;
class Products extends BaseController
{
public function index()
{
$model = model('ProductModel');
return view('products/index', ['products' => $model->findAll()]);
}
public function show($id = null)
{
$product = model('ProductModel')->find($id);
if ($product === null) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
return view('products/show', ['product' => $product]);
}
public function create()
{
if (! $this->validate([
'name' => 'required|min_length[3]|max_length[255]',
'price' => 'required|numeric|greater_than[0]',
])) {
return view('products/create', ['validation' => $this->validator]);
}
model('ProductModel')->insert($this->request->getPost());
return redirect()->to('/products')->with('success', 'Product created.');
}
}
Models
<?php
namespace App\Models;
use CodeIgniter\Model;
class ProductModel extends Model
{
protected $table = 'products';
protected $primaryKey = 'id';
protected $returnType = 'array';
protected $useSoftDeletes = true;
protected $useTimestamps = true;
protected $allowedFields = ['name', 'price', 'category_id', 'active'];
protected $validationRules = [
'name' => 'required|min_length[3]|max_length[255]',
'price' => 'required|numeric|greater_than[0]',
];
}
Critical: $allowedFields is mandatory — fields not listed are silently dropped on insert/update. This prevents mass assignment.
Query Builder
$db = db_connect();
$model = model('ProductModel');
$data = [
'products' => $model->where('active', 1)->paginate(20),
'pager' => $model->pager,
];
$results = $db->table('products')
->select('products.*, categories.name as category')
->join('categories', 'categories.id = products.category_id', 'left')
->where('products.active', 1)
->orderBy('products.name', 'ASC')
->get()
->getResultArray();
$db->transStart();
$db->table('orders')->insert($order_data);
$db->table('order_items')->insert($item_data);
$db->transComplete();
if (! $db->transStatus()) { }
Routing
$routes->get('/', 'Home::index');
$routes->get('products', 'Products::index');
$routes->get('products/(:num)', 'Products::show/$1');
$routes->post('products', 'Products::create');
$routes->resource('products');
$routes->group('api', ['namespace' => 'App\Controllers\Api', 'filter' => 'apiauth'], function ($routes) {
$routes->resource('products');
});
$routes->get('admin/(:any)', 'Admin::$1', ['filter' => 'auth']);
Input & Security
$name = $this->request->getPost('name');
$id = $this->request->getGet('id');
$data = $this->request->getJSON(true);
<?= esc($user_input) ?>
<?= esc($value, 'attr') ?>
<?= esc($value, 'js') ?>
Sessions
$session = session();
$session->set('user_id', $id);
$userId = $session->get('user_id');
$session->setFlashdata('success', 'Done.');
$session->setTempdata('otp', $code, 300);
Security Checklist (Both Versions)
CI3 → CI4 Quick Reference
| CI3 | CI4 |
|---|
$this->load->model('x') | model('XModel') or DI |
$this->load->view('x', $data) | return view('x', $data) |
$this->load->library('x') | service('x') or new X() |
$this->input->post('x') | $this->request->getPost('x') |
$this->db->get('table') | $db->table('table')->get() |
$query->result() / ->row() | $query->getResult() / ->getRow() |
$query->result_array() | $query->getResultArray() |
redirect('url') | return redirect()->to('url') |
show_404() | throw PageNotFoundException::forPageNotFound() |
$this->session->userdata('x') | session()->get('x') |
html_escape($x) | esc($x) |
$route['x'] = 'y' | $routes->get('x', 'Y::method') |
Hooks ($hook[]) | Filters (middleware) |
CI_Controller / CI_Model | BaseController / CodeIgniter\Model |
| No namespaces | PSR-4 namespaces |
| No CLI generator | php spark make:* |
See deployment.md for the full 30+ mapping migration table, server configuration, and production deployment checklist.