MVC Framework
MVC divides an application into three responsibilities: data, request handling, and presentation. CodeIgniter maps those responsibilities to models, controllers, and views.
The three layers
- Model. Reads and writes data, applies business rules, and returns arrays or objects. It should not output HTML.
- View. Contains markup. It displays prepared data and should not run database queries.
- Controller. Receives the request, validates input, calls models or services, and chooses the response.
A small example
// app/Controllers/Catalog.php
public function show(int $id)
{
$product = (new \App\Models\ProductModel())->find($id);
if ($product === null) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
return view('catalog/show', ['product' => $product]);
}
<!-- app/Views/catalog/show.php -->
<h1><?= esc($product['name']) ?></h1>
<p><?= esc($product['description']) ?></p>
Routing connects the URL
A route such as $routes->get('products/(:num)', 'Catalog::show/$1'); tells the framework which controller method receives the request. Routing is covered in more depth in Basic Concepts.
Why separation matters
When each layer has one job, changes stay local. You can redesign a page without touching SQL, or change a data source without rewriting templates. It also makes testing easier because models and services can be exercised without a browser.
A common mistake is putting queries in views or HTML in models. Keep the boundaries clear and the application stays maintainable as it grows.