codeigniter-knowledge
CodeIgniter Knowledge Base
Quick reference for CodeIgniter 4 framework patterns and PHP implementation guidelines. CodeIgniter 4 is a complete rewrite of the framework, built for PHP 8.1+ with namespaces, autoloading, and a modern MVC architecture. It remains lightweight and fast while providing the tools needed for professional application development.
Core Principles
MVC Architecture
Request → index.php → Bootstrap → Routing
→ Filters (before) → Controller → Service/Model → View
→ Filters (after) → Response
Key principles:
- Lightweight core with minimal overhead
- HMVC-capable via modules (app/Modules/)
- Services container for dependency management
- Filters as middleware equivalent
- Entity classes for data hydration
Quick Checklists
DDD-Compatible CI4 Project
- Domain logic extracted from Models into Domain layer
- Repository interfaces defined in Domain, implemented in Infrastructure
- Value Objects used instead of primitive types for domain concepts
- CI4 Models used only as persistence adapters
- Services registered in Config\Services for dependency injection
- Business rules not in Controllers or Filters
- Domain Events dispatched through domain
EventDispatcherInterfaceport (not CI4 Events directly) - Input validation at Controller level (not in Domain)
- Shield Groups/Permissions used only at Filter/Route level; Domain uses Specifications
- Queue job handlers delegate to Application UseCases
- Infrastructure components (Cache, HTTP Client, Email) accessed via domain ports
Clean Architecture Checks
- No
use CodeIgniter\...in Domain layer - No direct database access outside Repository implementations
- Controllers delegate to Application Services / Use Cases
- DTOs used for cross-layer data transfer
- Config\Services wires interfaces to implementations
- Entities are framework-free domain objects (not CI4 Entity)
Common Violations Quick Reference
| Violation | Where to Look | Severity |
|---|---|---|
| Business logic in Controller | app/Controllers/*.php |
Critical |
| Domain depends on CI4 framework | use CodeIgniter\ in Domain classes |
Critical |
| Direct DB queries in Controller | $this->db-> in Controllers |
Critical |
| Model contains business rules | Complex if/switch in Model methods |
Warning |
| Missing input validation | Controllers without $this->validate() |
Warning |
| God Controller | Controllers > 300 lines | Warning |
| Shield UserIdentity in Domain | use CodeIgniter\Shield in Domain layer |
Critical |
| Queue in Domain | service('queue') in Domain layer |
Critical |
| Cache/HTTP Client in Domain | CacheInterface or CURLRequest in Domain |
Critical |
| Missing queue resilience | Jobs without $retryAfter/$tries |
Warning |
| Hardcoded config values | Magic strings instead of config() |
Info |
PHP 8.4 CodeIgniter Patterns
Controller (Thin, Delegates to Service)
<?php
declare(strict_types=1);
namespace App\Controllers\Api;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
final class OrderController extends BaseController
{
public function __construct(
private readonly OrderService $orderService,
) {}
public function show(string $id): ResponseInterface
{
$order = $this->orderService->findById($id);
return $this->response->setJSON(['id' => $order->id, 'status' => $order->status->value]);
}
}
Service (Application Layer)
<?php
declare(strict_types=1);
namespace App\Services;
final readonly class OrderService
{
public function __construct(
private OrderRepositoryInterface $orders,
private EventDispatcherInterface $events,
) {}
public function confirm(string $orderId): void
{
$order = $this->orders->findById($orderId)
?? throw new OrderNotFoundException($orderId);
$order->confirm();
$this->orders->save($order);
$this->events->dispatch(new OrderConfirmedEvent($order->id()));
}
}
Model as Repository Adapter
<?php
declare(strict_types=1);
namespace App\Models;
use CodeIgniter\Model;
final class OrderModel extends Model
{
protected $table = 'orders';
protected $primaryKey = 'id';
protected $returnType = OrderEntity::class;
protected $allowedFields = ['customer_id', 'status', 'total', 'notes'];
protected $useTimestamps = true;
}
References
For detailed information, load these reference files:
references/architecture.md-- CI4 MVC structure, modules, directory layoutreferences/ddd-integration.md-- Domain extraction, Repository pattern, Value Objects in CI4references/routing-http.md-- Controllers, Filters, RESTful routing, validationreferences/persistence.md-- CI4 Model, Query Builder, Entity classes, migrationsreferences/dependency-injection.md-- Services class, custom registration, DI patternsreferences/testing.md-- CIUnitTestCase, DatabaseTestTrait, feature testing, mockingreferences/security.md-- Shield authentication/authorization, Filters, CSRF, password hashing with DDDreferences/event-system.md-- CI4 Events, domain event dispatching, lifecycle events, testingreferences/queue.md-- codeigniter4/queue jobs, workers, retry, chained jobs, queue events with DDDreferences/infrastructure-components.md-- Cache, CURLRequest HTTP client, Email, Throttler with DDD portsreferences/antipatterns.md-- Common violations with detection patterns and fixes
More from dykyi-roman/awesome-claude-code
psr-overview-knowledge
PHP Standards Recommendations (PSR) overview knowledge base. Provides comprehensive reference for all accepted PSRs including PSR-1,3,4,6,7,11,12,13,14,15,16,17,18,20. Use for PSR selection decisions and compliance audits.
22detect-code-smells
Detects code smells in PHP codebases. Identifies God Class, Feature Envy, Data Clumps, Long Parameter List, Long Method, Primitive Obsession, Message Chains, Inappropriate Intimacy. Generates actionable reports with refactoring recommendations.
15clean-arch-knowledge
Clean Architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Clean Architecture and Hexagonal Architecture audits.
15ddd-knowledge
DDD architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Domain-Driven Design audits.
14testing-knowledge
Testing knowledge base for PHP 8.4 projects. Provides testing pyramid, AAA pattern, naming conventions, isolation principles, DDD testing guidelines, and PHPUnit patterns.
12bug-root-cause-finder
Root cause analysis methods for PHP bugs. Provides 5 Whys technique, fault tree analysis, git bisect guidance, and stack trace parsing.
12