Introduction
Aeros uses a Service Container for dependency injection and service management. The container allows you to register services, manage dependencies, and access them throughout your application, including in controllers.The Service Container
The Service Container in Aeros is the central registry for all services and dependencies. It’s accessible via the globalapp() helper function.
Container Architecture
The Service Container (Aeros\Src\Classes\ServiceContainer) provides:
- Service Registration: Register classes and callables as services
- Singleton Pattern: Access services as singletons throughout the application
- Service Providers: Bootstrap services during application initialization
- Dependency Resolution: Automatically resolve dependencies
Accessing Services in Controllers
Using the app() Helper
The most common way to access services is through theapp() helper:
UserController.php
Available Core Services
Aeros provides several built-in services accessible viaapp():
app()->router
app()->router
The routing service for handling HTTP routes and dispatching requests.
app()->request
app()->request
Request service for accessing HTTP request data.
app()->response
app()->response
Response service for formatting and returning HTTP responses.
app()->db
app()->db
Database service for executing queries.
app()->view
app()->view
View service for rendering templates.
app()->session
app()->session
Session management service.
app()->cache
app()->cache
Cache service supporting multiple drivers.
app()->config
app()->config
Configuration service for accessing config files.
Constructor Injection
While Aeros doesn’t automatically inject dependencies into controller constructors, you can manually resolve dependencies in the constructor:ProductController.php
Registering Custom Services
Using Service Providers
Create custom service providers to register your services:app/Providers/RepositoryServiceProvider.php
config/app.php:
config/app.php
Using Singletons
Register services as singletons to ensure only one instance exists:AppServiceProvider.php
PaymentController.php
Practical Examples
Repository Pattern
Use dependency injection with the repository pattern:app/Repositories/UserRepository.php
UserController.php
Service Classes
Create service classes for complex business logic:app/Services/OrderService.php
AppServiceProvider.php
OrderController.php
External API Integration
Create services for external API integrations:app/Services/EmailService.php
Helper Functions
Aeros provides helper functions that internally use the service container:UserController.php
Best Practices
Use service providers for registration
Use service providers for registration
Register all custom services in dedicated service providers rather than in controllers or routes.
Keep controllers thin
Keep controllers thin
Move complex business logic to service classes and repositories. Controllers should only coordinate between services and return responses.
Use singletons for stateless services
Use singletons for stateless services
Register services as singletons when they don’t maintain state between requests (e.g., API clients, repositories).
Leverage helper functions
Leverage helper functions
Use Aeros helper functions (
db(), cache(), session()) for cleaner, more readable code.Type hint service dependencies
Type hint service dependencies
When injecting services in constructors, use type hints for better IDE support and clarity.
Advanced: Custom Service Container Methods
The Service Container provides several methods for advanced usage:Next Steps
Basic Controllers
Review the fundamentals of creating and using controllers.
Resource Controllers
Learn about RESTful resource controllers and CRUD operations.