Analyzing Design Patterns in CodeIgniter 4 |
CodeIgniter 4 employs several design patterns to enhance code organization, reusability, and maintainability. Here my note of the (7+2) prominent patterns:
1. Singleton Pattern Purpose: Ensures that a class has only one instance and provides a global point of access to it. Usage: CodeIgniter's CI_Loader class is a classic example of a singleton. It's used to load other classes and components within the framework. 2. Dependency Injection Purpose: Promotes loose coupling between classes by passing dependencies as parameters to constructors or methods. Usage: CodeIgniter uses dependency injection to inject necessary services into controllers and other classes. For instance, a controller might be injected with a database connection or a session handler. 3. Front Controller Pattern Purpose: Centralizes request handling and routing. Usage: The index.php file in CodeIgniter acts as the front controller. It intercepts all incoming requests, routes them to the appropriate controller, and handles the response. 4. Model-View-Controller (MVC) PatternĀ Purpose: Separates concerns into three interconnected components: Model (data), View (presentation), and Controller (logic). Usage: CodeIgniter adheres to the MVC architecture. Controllers handle user requests, models interact with data, and views present the output. 5. Observer Pattern Purpose: Defines a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically. Usage: While not explicitly implemented in CodeIgniter's core, the observer pattern can be used to implement features like caching or event handling. 6. Factory Pattern Purpose: Provides a way to create objects without specifying their exact class. Usage: CodeIgniter's CI_Loader class can be seen as a factory for creating instances of other classes based on their names. 7. Facade Pattern Purpose: Provides a unified interface to a set of interfaces in a subsystem. Usage: While not a direct implementation, CodeIgniter's CI_Loader class can be considered a facade, simplifying the process of loading and accessing various components. Additional: - Middleware: CodeIgniter's middleware functionality can be seen as an application of the Chain of Responsibility pattern. - Routing: The routing system in CodeIgniter employs a combination of patterns, including the Strategy pattern for different routing methods and the Composite pattern for grouping routes. Are you interested in diving deeper into CodeIgniter 4 patterns used?
CI_Loader and Middleware do not exists in CI4.
Probably Middleware means Controller Filters, but what is CI_Loader ?
@kenjis
What I mean by CI_loader is like the way we load libraries, helpers, views, models. https://codeigniter.com/user_guide/model...ing-models Middleware, perhaps like filters (before and after controller flow), Codeigniter Shield? CMIIW
model() and config() are wrappers for Factories.
Factories instantiate a specified class and share the instance. |
Welcome Guest, Not a member yet? Register Sign In |