Configuration
Overview
The Forge Framework provides a flexible and layered configuration system that allows you to manage settings at different levels of your application. Configuration can be handled through environment variables, application configuration files, and module-specific configurations.
Environment Configuration
Environment-specific configuration is managed through environment variables, typically defined in your .env
file. The framework provides an env-example
file as a template:
APP_ENV=development
FORGE_APP_DEBUG=true
FORGE_APP_KEY=
FORGE_APP_NAME=Forge
# Database Configuration
FORGE_DB_CONNECTION=sqlite
FORGE_DB_HOST=127.0.0.1
FORGE_DB_PORT=3306
FORGE_DB_DATABASE=forge
FORGE_DB_USERNAME=root
FORGE_DB_PASSWORD=root
Environment variables are loaded and processed by the Config class, which automatically converts them into appropriate configuration values.
Application Configuration
Application-level configuration is defined in your app's configuration files, located in the apps/MyApp/config
directory. The main configuration file is app.php
:
return [
'name' => 'MyApp',
'key' => \Forge\Core\Helpers\App::env("FORGE_APP_KEY"),
'middleware' => [
\Forge\Http\Middleware\ErrorHandlingMiddleware::class,
\MyApp\Middleware\StartSessionMiddleware::class,
],
'paths' => [
'resources' => [
'views' => 'apps/MyApp/resources/views',
'components' => 'apps/MyApp/resources/components',
],
'database' => [
'migrations' => 'apps/MyApp/Database/Migrations',
'seeders' => 'apps/MyApp/Database/Seeders'
],
],
];
The application configuration is loaded during the bootstrap process in MyApp.php
:
public function register(Container $container): void
{
$config = App::config();
// Access configuration values
$routeFile = $config->get('routes.web');
}
Module Configuration
Modules can provide their own configuration files in their config
directory. These configurations can be published to the application's config directory for customization.
Configuration Loading
The framework's Config class handles loading and merging configurations from multiple sources:
- Module default configurations
- Application-specific configurations
- Environment variables
Configuration Validation
The ConfigValidator ensures that configuration values meet the required schema:
class ConfigValidator
{
public function validate($config, array $schemas): void
{
foreach ($schemas as $key => $schema) {
$this->validateNode($key, $schema, $configArray);
}
}
}
Publishing Configuration
Module configurations can be published to your application using the publish
command:
php forge.php publish module-name --type=config
Available publishing options:
--type=config
: Publish configuration files--type=views
: Publish view templates--type=components
: Publish UI components--type=assets
: Publish static assets--type=all
: Publish all publishable resources
Published configurations will be placed in your application's config directory, allowing you to customize the module's behavior while maintaining the original defaults.