Advertisement
Google Ad Slot: content-top
CodeIgniter MVC Framework
What is CodeIgniter MVC Framework?
CodeIgniter is a lightweight PHP framework that follows the MVC (Model–View–Controller) architectural pattern.
It helps you organize your application code by separating:
- Model → Handles data & database logic.
- View → Handles the presentation (HTML/CSS/JS).
- Controller → Handles requests and coordinates between Model & View.
CodeIgniter Folder Structure
When you install CodeIgniter 4, your main app code lives in app/ directory:
app/ ├── Controllers/ ├── Models/ ├── Views/
How MVC Works in CodeIgniter
- User visits a URL → Controller is triggered.
- Controller calls the Model for data.
- Model returns data to the Controller.
- Controller sends data to a View.
- View displays the data in a browser.
For database and table creation, refer MySQL Database before continuing further.
Example: Displaying a Product List
1. Model
app/Models/ProductModel.php
Purpose: Fetches product data from the database.
2. Controller
app/Controllers/Product.php
Purpose: Loads product data from ProductModel and passes it to the view.
3. View
app/Views/products_list.php
Purpose: Displays the product list.
Routing to the Controller
app/Config/Routes.php
$routes->get('products', 'Product::index');
Flow Diagram of MVC in CodeIgniter
Browser → Controller → Model → Controller → View → Browser
Advantages of MVC in CodeIgniter
- Clear separation of logic and presentation.
- Easier to maintain and scale.
- Reusable components.
- Supports clean URLs via routing.