Are you looking to build powerful PHP web applications but feeling overwhelmed by where to start? Well, I’ve got fantastic news for you! Codeigniter is absolutely the framework you need to master. I’ve been using it for years, and I can tell you firsthand that it’s one of the most beginner-friendly yet powerful PHP frameworks out there.
In this comprehensive tutorial, I’ll walk you through everything you need to know to start building amazing web applications with Codeigniter 4, the latest version as of now. No confusing jargon, just straightforward guidance that’ll have you coding like a pro in no time.
What Makes Codeigniter So Special?
Before we dive into the code, let me tell you why Codeigniter stands out from other PHP frameworks:
- Lightning Fast: Codeigniter is incredibly lightweight and performs much faster than most other frameworks.
- Easy Learning Curve: The documentation is superb, and the framework follows logical patterns that are easy to grasp.
- Zero Configuration: You can set up and start developing with minimal configuration.
- Built-in Security: The framework comes with powerful tools to protect your applications from common web attacks.
- Community Support: A massive community means you’ll never get stuck on a problem for long.
Trust me, once you start using Codeigniter, you’ll wonder how you ever developed without it!
Prerequisites
Before we begin, make sure you have:
- Basic knowledge of PHP (you should understand variables, functions, and object-oriented concepts. Explore our PHP Tutorials to level up your skill!)
- Understanding of the MVC design pattern (I’ll explain this more as we go)
- A local development environment (XAMPP, WAMP, or similar)
- PHP 8.1 or higher installed
- Composer installed (the PHP dependency manager)
Understanding MVC Architecture

Codeigniter uses the Model-View-Controller (MVC) pattern, which separates your application into three interconnected components:
- Models: Handle your data and business logic
- Views: Display information to users
- Controllers: Process user requests and coordinate between models and views
This separation makes your code more organized, easier to maintain, and simpler to test. You’ll absolutely love how clean your codebase becomes!
Installing Codeigniter 4
Let’s get started by installing Codeigniter 4. There are two ways to do this:
Method 1: Using Composer (Recommended)
Open your terminal or command prompt and run:
composer create-project codeigniter4/appstarter my-project
This creates a new Codeigniter project named “my-project” in the current directory.
Method 2: Manual Download
- Download the latest version from Codeigniter’s official website
- Extract the files to your web server’s root directory
- Rename the directory to your project name
I strongly recommend using the Composer method as it makes updating Codeigniter much easier in the future.
Directory Structure
After installation, you’ll see the following directory structure:
my-project/
├── app/
│ ├── Config/
│ ├── Controllers/
│ ├── Models/
│ ├── Views/
│ └── ...
├── public/
├── system/
├── writable/
├── tests/
└── vendor/Code language: PHP (php)
Let’s understand what each important directory does:
- app: This is where all YOUR code goes – controllers, models, views, config files, etc.
- public: The only directory that should be publicly accessible. Contains index.php and assets like CSS, JS, and images.
- system: Core framework files – NEVER modify these files directly!
- writable: Contains files that need write access (logs, cache, etc.)
- vendor: Contains third-party dependencies
Basic Configuration
Before we start building, let’s configure a few basics:
1. Environment Setup
Rename the env file to .env in your project root. Open it and set:
CI_ENVIRONMENT = development
This enables detailed error reporting during development.
2. Database Configuration
In the .env file, configure your database settings:
database.default.hostname = localhost
database.default.database = my_database
database.default.username = root
database.default.password = your_password
database.default.DBDriver = MySQLiCode language: PHP (php)
3. URL Configuration
Still in the .env file, set your base URL:
app.baseURL = 'http://localhost/my-project/public/'Code language: JavaScript (javascript)
Creating Your First Controller
Controllers are the heart of your application, handling user requests and providing responses. Let’s create our first controller:
- Create a file named
Blog.phpin theapp/Controllersdirectory:
<?php
namespace App\Controllers;
class Blog extends BaseController
{
public function index()
{
echo "Hello World! Welcome to my blog.";
}
}Code language: HTML, XML (xml)
Now, start your local server and navigate to http://localhost/my-project/public/blog. You should see “Hello World! Welcome to my blog.” This simple controller demonstrates how easy it is to create pages in Codeigniter! 🎉
Tip💡: Explore All CodeIgniter Tutorials by CodeSamplez.com
Working with Views
Displaying raw text isn’t very useful. Let’s create a view to render HTML properly:
- Create a directory
app/Views/blog - Create a file
index.phpin this directory:
<!DOCTYPE html>
<html>
<head>
<title>My Awesome Blog</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #333;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
}
.article {
margin-bottom: 30px;
border-bottom: 1px dashed #ccc;
padding-bottom: 20px;
}
.article h2 {
margin-bottom: 5px;
}
.meta {
color: #777;
font-size: 0.9em;
margin-bottom: 15px;
}
</style>
</head>
<body>
<h1>Welcome to My Blog</h1>
<?php foreach($articles as $article): ?>
<div class="article">
<h2><?= $article->title ?></h2>
<div class="meta">By <?= $article->author ?></div>
<p><?= $article->content ?></p>
</div>
<?php endforeach; ?>
</body>
</html>Code language: HTML, XML (xml)
- Now, update your controller to use this view:
<?php
namespace App\Controllers;
class Blog extends BaseController
{
public function index()
{
// Data to be passed to the view
$data = [
'articles' => [
(object)[
'title' => 'Getting Started with Codeigniter 4',
'author' => 'John Doe',
'content' => 'Codeigniter 4 is an amazing framework that makes PHP development a breeze.'
],
(object)[
'title' => 'MVC Pattern Explained',
'author' => 'Jane Smith',
'content' => 'The Model-View-Controller pattern separates concerns and makes your code more maintainable.'
]
]
];
// Load the view with data
return view('blog/index', $data);
}
}Code language: HTML, XML (xml)
When you refresh the page, you’ll see a nicely formatted blog with two articles. That’s the power of views – they separate your HTML from your PHP logic!
Creating a Model
Models handle your data operations. Let’s create a model to manage our blog articles:
- Create a file named
BlogModel.phpin theapp/Modelsdirectory:
<?php
namespace App\Models;
use CodeIgniter\Model;
class BlogModel extends Model
{
// In a real application, you'd connect to a database
public function getArticles()
{
// For now, we'll return dummy data
return [
(object)[
'title' => 'Getting Started with Codeigniter 4',
'author' => 'John Doe',
'content' => 'Codeigniter 4 is an amazing framework that makes PHP development a breeze.'
],
(object)[
'title' => 'MVC Pattern Explained',
'author' => 'Jane Smith',
'content' => 'The Model-View-Controller pattern separates concerns and makes your code more maintainable.'
],
(object)[
'title' => 'Working with Databases in CI4',
'author' => 'Mike Johnson',
'content' => 'Codeigniter makes database operations simple with its powerful database library.'
]
];
}
}Code language: HTML, XML 