Meet CodeIgniter
CodeIgniter is a small and fast framework — a ready-made skeleton for a PHP site. It has little hidden automation: an address leads to a class method, the method takes data and outputs a template. That makes it easy to learn right after plain PHP. The course targets CodeIgniter 4.5 and newer.
What you need
- PHP 8.1 or newer with the
intlandmbstringextensions; - Composer — the program that downloads PHP packages;
- a MySQL database (needed from the second chapter on).
Creating a project
One Composer command creates the project. Throughout the course we build a blog, so the folder is called blog:
composer create-project codeigniter4/appstarter blog
cd blog
php spark serveThe first command downloads the framework into the blog folder, the last one starts the built-in development server. Open http://localhost:8080 and the CodeIgniter welcome page appears.
Project folders
blog/
├── app/
│ ├── Config/
│ ├── Controllers/
│ ├── Database/
│ ├── Models/
│ └── Views/
├── public/
├── tests/
├── vendor/
├── writable/
├── env
└── spark| Folder or file | What is inside |
|---|---|
app | All your code: settings, controllers, models, templates |
public | The site root: index.php, styles, images |
writable | Files the site creates itself: cache, logs, sessions |
tests | Automated tests |
vendor | The framework and packages installed by Composer |
env | A sample settings file |
spark | The framework's console program |
Almost all work happens in the app folder. Nothing is changed in vendor: its contents are overwritten on update.
Development mode
The settings of your own computer are kept in the .env file in the project root. Copy the sample env under the name .env and add one line:
CI_ENVIRONMENT = developmentIn development mode errors are shown in detail and a debug toolbar appears at the bottom of the page. There is no need to restart the server — just reload the page.
The first page
The welcome page is produced by the class in app/Controllers/Home.php. Replace its return value with a string:
// app/Controllers/Home.php
namespace App\Controllers;
class Home extends BaseController
{
public function index(): string
{
return 'Hello, CodeIgniter!';
}
}Reload http://localhost:8080 — the text "Hello, CodeIgniter!" replaces the welcome page. How the address is tied to this class is explained in the lesson about routes.
Common mistake
The sample file is called env, without a dot, and the framework does not read it. Settings written there change nothing. They work only in the .env file — with a leading dot — and the line must not start with #, which marks a comment.