Overview
The Container is the heart of ForkBB’s architecture. It provides:- Dependency injection for loose coupling
- Service registration with shared and multiple instances
- Lazy loading for performance
- Parameter substitution for configuration
- Factory methods for complex initialization
The Container is based on the artoodetoo/container library.
Container Class
The Container class is located atapp/Core/Container.php:
app/Core/Container.php
Service Registration
Services are registered in configuration files:app/config/main.dist.php
Service Types
- Simple Parameters
- Multiple Instances
- Factory Methods
Parameter Substitution
The container supports parameter substitution using%param% syntax:
Whole String Substitution
Whole String Substitution
Replace entire parameter value.
Partial String Substitution
Partial String Substitution
Embed parameters in strings.
Service References
Service References
Reference other services with
@service.Nested Parameters
Nested Parameters
Access nested array values with dot notation.
Accessing Services
Basic Access
Nested Access
Dependency Injection
All services automatically receive the container:Configuration Structure
Directory Paths
Core Services
Model Managers
Service Lifecycle
Example: Registering a Custom Service
Step 1: Create Service Class
app/MyExtension/MyService.php
Step 2: Register in Container
config/main.php
Step 3: Use Service
Best Practices
Use Type Hints
Use Type Hints
Always type hint the Container parameter.
Lazy Load Services
Lazy Load Services
Services are automatically lazy loaded. Don’t instantiate in constructor.
Use Shared for Singletons
Use Shared for Singletons
Use Multiple for Models
Use Multiple for Models
Domain models should be multiple instances.
Check Initialization
Check Initialization
Check if expensive services are initialized before cleanup.
Advanced Features
Dynamic Service Registration
Service Override
Next Steps
Architecture
Understand how the container fits into ForkBB
Models
Create services that use models
Controllers
Access services in controllers