Overview
CodeIgniter is a powerful PHP framework with a small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.
Key Features
- Lightweight and fast
- MVC architecture
- Clear documentation
- Built-in libraries and helpers
- Security and XSS filtering
- Easy error handling
Installation
Download CodeIgniter from the official website and extract it to your web server directory. Or use Composer:
composer create-project codeigniter4/appstarter project-root
MVC Architecture
CodeIgniter uses the Model-View-Controller (MVC) pattern to separate application logic from presentation.
- Model: Handles data and database logic.
- View: Displays data to the user.
- Controller: Processes input and updates models/views.
Routing
Routes define how URLs map to controllers. Configure routes in app/Config/Routes.php
:
$routes->get('/', 'Home::index');
Controllers
Controllers handle requests and return responses. Example:
class Home extends BaseController {
public function index() {
return view('welcome_message');
}
}
Models
Models interact with the database. Example:
class UserModel extends Model {
protected $table = 'users';
}
Views
Views display data to the user. Example:
echo view('welcome_message');
Sessions & Cookies
Manage user sessions and cookies easily:
$session = session();
$session->set('user_id', 1);
Helpers
Helpers are utility functions for common tasks. Load a helper:
helper('url');
Error Handling
CodeIgniter provides error logging and custom error pages.
File Handling
Upload and manage files using built-in libraries.
Send emails using the Email library.
$email = \Config\Services::email();
$email->setTo('user@example.com');
$email->setSubject('Subject');
$email->setMessage('Message');
$email->send();
Database
Interact with databases using the Query Builder or raw SQL.
$db = \Config\Database::connect();
$query = $db->query('SELECT * FROM users');
Hooks
Hooks allow you to execute scripts at specific points in the application lifecycle.