GitHub

Configuration

TinyMVC uses a centralized configuration system through the env.php file, which contains all application settings.

Configuration File

The main configuration file is located at env.php (created from env.example.php):

<?php

return [
     // Debugging settings
    'debug' => true,

    // Directory paths
    'storage_dir' => __DIR__ . '/storage',
    'cache_dir' => __DIR__ . '/storage/cache', 
    'upload_dir' => __DIR__ . '/public/uploads',
    'views_dir' => __DIR__ . '/resources/views',
    'lang_dir' => __DIR__ . '/resources/languages',

    // URL settings
    'media_url' => '/uploads/',
    'asset_url' => '/assets/',

    // Localization settings
    'lang' => 'en',

    // Security settings
    'app_key' => 'e32e4a35fce965947481cdad2db2a4e4',

    // Database connection settings
    'database' => [
        'driver' => 'sqlite',
        'file' => __DIR__ . '/database/sqlite.db',
    ],

    // Mail server configurations
    'mail' => [
        'mailer' => [
            'address' => '{MAILER_ADDRESS}',
            'name' => '{MAILER_NAME}'
        ],
        'reply' => [
            'address' => '{REPLY_ADDRESS}',
            'name' => '{REPLY_NAME}'
        ],
        'smtp' => [
            'enabled' => false,
            'host' => '{SMTP_HOST}',
            'port' => 2525,
            'username' => '{SMTP_USERNAME}',
            'password' => '{SMTP_PASSWORD}',
            'encryption' => 'tls|ssl',
        ],
    ],
];

Accessing Configuration

Using Helper Function

// Get configuration value
$debug = config('debug');

// Get nested value
$dbDriver = config('database.driver');

// Get with default value
$timezone = config('app.timezone', 'UTC');

Using Application Methods

// In service providers or controllers
$debug = app()->getEnv('debug');

// Get nested value using dot notation
$dbHost = app()->getEnv('database.host');

Modifying Configuration

// Set single value
config(['debug' => false]);

// Set nested value
config(['database.host' => '127.0.0.1']);

// Using application methods
app()->setEnv('debug', false);
app()->mergeEnv([
    'database' => [
        'host' => '127.0.0.1',
        'port' => 3306
    ]
]);

Configuration Structure

Standard configuration sections include:

  • Debugging: Application debug mode
  • Directories: Paths to important directories
  • URLs: Base URLs for assets and media
  • Localization: Default language settings
  • Security: Encryption keys and security settings
  • Database: Connection parameters
  • Mail: Email server configurations

Environment Setup

  1. Copy env.example.php to env.php
  2. Update values with your actual configuration
  3. Generate application key: php spark key:generate
  4. Never commit env.php to version control

Best Practices

  • Keep sensitive data (keys, passwords) in env.php
  • Use different configurations for development/production
  • Group related settings in nested arrays
  • Set defaults in code for optional configuration

Full Example

// Setting configuration in service provider
public function register(Container $container)
{
    // Override mail configuration
    config([
        'mail' => [
            'smtp' => [
                'host' => 'mail.production.com',
                'port' => 465,
                'encryption' => 'ssl'
            ]
        ]
    ]);
    
    // Conditionally set debug mode
    config(['debug' => config('app.env') !== 'production']);
}

// Accessing configuration in controller
public function index()
{
    return view('welcome', [
        'assetUrl' => config('asset_url'),
        'debugMode' => config('debug', false)
    ]);
}



Quick Start - Get started with TinyMVC