Configuration
Environment variables and configuration options
DALT.PHP uses environment variables and configuration files to manage application settings.
Environment Variables
Setup
Copy Example File
cp .env.example .envNever commit .env to version control! It contains sensitive credentials.
Application Configuration
APP_ENV
Defines the application environment.
APP_ENV=localOptions:
local- Development environmentstaging- Staging/testing environmentproduction- Production environment
Usage:
$env = config('app.env');
if ($env === 'production') {
// Production-specific code
}APP_DEBUG
Controls debug mode and error display.
APP_DEBUG=trueOptions:
true- Show detailed errors (development)false- Hide errors (production)
Always set to false in production! Debug mode exposes sensitive information.
What it affects:
- Error messages (detailed vs generic)
- Stack traces (shown vs hidden)
- Debug dumps (dd() output)
Database Configuration
SQLite (Default)
Simplest setup for development and learning.
DB_DRIVER=sqlite
DB_DATABASE=database/app.sqliteAdvantages:
- Zero configuration
- Single file database
- Perfect for learning
- Easy to reset
File location: database/app.sqlite
Reset database:
php artisan migrate:freshPostgreSQL
Recommended for production and advanced features.
DB_DRIVER=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_NAME=dalt_php_app
DB_USERNAME=postgres
DB_PASSWORD=your_password
DB_CHARSET=utf8Setup PostgreSQL:
# Install PostgreSQL
sudo apt install postgresql
# Create database
sudo -u postgres createdb dalt_php_app
# Create user
sudo -u postgres createuser -P dalt_user
# Grant permissions
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE dalt_php_app TO dalt_user;"Connection string format:
pgsql:host=127.0.0.1;port=5432;dbname=dalt_php_appDatabase Options
DB_DRIVER
Database type to use.
DB_DRIVER=sqliteSupported drivers:
sqlite- SQLite (default)pgsql- PostgreSQL
DB_HOST
Database server hostname.
DB_HOST=127.0.0.1Common values:
127.0.0.1- Local serverlocalhost- Local server (may use Unix socket)db- Docker container name- Remote IP address
DB_PORT
Database server port.
DB_PORT=5432Default ports:
- PostgreSQL:
5432 - MySQL:
3306(not officially supported)
DB_NAME
Database name to connect to.
DB_NAME=dalt_php_appMust exist before running migrations.
DB_USERNAME
Database user for authentication.
DB_USERNAME=postgresUser must have permissions to:
- Create tables
- Insert/update/delete data
- Create indexes
DB_PASSWORD
Database user password.
DB_PASSWORD=your_secure_passwordUse strong passwords in production. Never use default passwords.
DB_CHARSET
Character encoding for database connection.
DB_CHARSET=utf8Common values:
utf8- Standard UTF-8utf8mb4- UTF-8 with emoji support
DB_DATABASE
SQLite database file path (SQLite only).
DB_DATABASE=database/app.sqliteRelative to project root. Can be absolute path:
DB_DATABASE=/var/www/data/app.sqliteConfiguration Files
config/app.php
Application-level settings.
<?php
return [
'name' => 'DALT.PHP Framework',
'env' => $_ENV['APP_ENV'] ?? 'local',
'debug' => $_ENV['APP_DEBUG'] ?? true,
];Access in code:
$appName = config('app.name');
$isDebug = config('app.debug');config/database.php
Database connection settings.
<?php
return [
'database' => [
'driver' => $_ENV['DB_DRIVER'] ?? 'sqlite',
'host' => $_ENV['DB_HOST'] ?? '127.0.0.1',
'port' => $_ENV['DB_PORT'] ?? 5432,
'dbname' => $_ENV['DB_NAME'] ?? 'dalt_php_app',
'username' => $_ENV['DB_USERNAME'] ?? 'postgres',
'password' => $_ENV['DB_PASSWORD'] ?? '',
'charset' => $_ENV['DB_CHARSET'] ?? 'utf8',
'database' => $_ENV['DB_DATABASE'] ?? base_path('database/app.sqlite'),
]
];Access in code:
$dbConfig = config('database.database');
$driver = $dbConfig['driver'];Environment-Specific Configuration
Development (.env)
# Development settings
APP_ENV=local
APP_DEBUG=true
# SQLite for simplicity
DB_DRIVER=sqlite
DB_DATABASE=database/app.sqliteStaging (.env)
# Staging settings
APP_ENV=staging
APP_DEBUG=true
# PostgreSQL for testing
DB_DRIVER=pgsql
DB_HOST=staging-db.example.com
DB_PORT=5432
DB_NAME=dalt_staging
DB_USERNAME=staging_user
DB_PASSWORD=staging_passwordProduction (.env)
# Production settings
APP_ENV=production
APP_DEBUG=false
# PostgreSQL with secure credentials
DB_DRIVER=pgsql
DB_HOST=prod-db.example.com
DB_PORT=5432
DB_NAME=dalt_production
DB_USERNAME=prod_user
DB_PASSWORD=very_secure_password_hereProduction checklist:
- Set
APP_DEBUG=false - Use strong database passwords
- Use PostgreSQL (not SQLite)
- Restrict database user permissions
- Enable SSL for database connections
Accessing Configuration
Using config() Helper
// Get single value
$appName = config('app.name');
// Get nested value
$dbDriver = config('database.database.driver');
// Get with default
$timeout = config('app.timeout', 30);
// Get entire config file
$appConfig = config('app');Using $_ENV Directly
// Access environment variable
$debug = $_ENV['APP_DEBUG'] ?? false;
// Check if variable exists
if (isset($_ENV['DB_PASSWORD'])) {
// Use password
}In Controllers
<?php
use function Core\Helpers\config;
class PostController
{
public function index()
{
$isDebug = config('app.debug');
if ($isDebug) {
dd('Debug mode enabled');
}
// Your code
}
}Custom Configuration
Creating New Config Files
Create Config File
Create config/services.php:
<?php
return [
'mail' => [
'driver' => $_ENV['MAIL_DRIVER'] ?? 'smtp',
'host' => $_ENV['MAIL_HOST'] ?? 'localhost',
'port' => $_ENV['MAIL_PORT'] ?? 587,
],
'cache' => [
'driver' => $_ENV['CACHE_DRIVER'] ?? 'file',
'path' => $_ENV['CACHE_PATH'] ?? base_path('storage/cache'),
],
];Add Environment Variables
Add to .env:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
CACHE_DRIVER=file
CACHE_PATH=storage/cacheAccess Configuration
$mailDriver = config('services.mail.driver');
$cacheDriver = config('services.cache.driver');Common Configurations
Local Development
# .env
APP_ENV=local
APP_DEBUG=true
DB_DRIVER=sqlite
DB_DATABASE=database/app.sqliteBest for:
- Learning DALT.PHP
- Quick prototyping
- Testing features
Docker Development
# .env
APP_ENV=local
APP_DEBUG=true
DB_DRIVER=pgsql
DB_HOST=db
DB_PORT=5432
DB_NAME=dalt
DB_USERNAME=dalt
DB_PASSWORD=secretDocker Compose:
services:
app:
build: .
environment:
- APP_ENV=local
- DB_HOST=db
db:
image: postgres:16-alpine
environment:
- POSTGRES_DB=dalt
- POSTGRES_USER=dalt
- POSTGRES_PASSWORD=secretProduction Server
# .env
APP_ENV=production
APP_DEBUG=false
DB_DRIVER=pgsql
DB_HOST=10.0.1.50
DB_PORT=5432
DB_NAME=dalt_prod
DB_USERNAME=dalt_prod_user
DB_PASSWORD=complex_secure_password_123!
DB_CHARSET=utf8Configuration Caching
DALT.PHP loads configuration on every request. For production optimization:
Option 1: OPcache
Enable PHP OPcache to cache compiled PHP files:
; php.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000Option 2: Environment Variables
Set environment variables at the system level instead of .env:
# /etc/environment
export APP_ENV=production
export APP_DEBUG=false
export DB_DRIVER=pgsqlTroubleshooting
Configuration Not Loading
Problem: Changes to .env not taking effect.
Solution:
# Restart PHP server
php artisan serve
# Or restart PHP-FPM
sudo systemctl restart php8.2-fpmDatabase Connection Failed
Problem: Cannot connect to database.
Check:
- Database server is running
- Credentials are correct
- Database exists
- User has permissions
Test connection:
# PostgreSQL
psql -h 127.0.0.1 -U postgres -d dalt_php_app
# SQLite
sqlite3 database/app.sqlite ".tables"Permission Denied
Problem: Cannot write to database file.
Solution:
# Fix permissions
chmod 664 database/app.sqlite
chmod 775 database/
# Or use web server user
sudo chown www-data:www-data database/app.sqliteMissing .env File
Problem: Application fails with "environment variable not found".
Solution:
# Copy example file
cp .env.example .env
# Edit with your values
nano .envSecurity Best Practices
1. Never Commit .env
Add to .gitignore:
.env
.env.backup
.env.production2. Use Strong Passwords
# Bad
DB_PASSWORD=password
# Good
DB_PASSWORD=Kx9$mP2#vL8@nQ5!wR73. Restrict Database Permissions
-- Create user with limited permissions
CREATE USER dalt_user WITH PASSWORD 'secure_password';
-- Grant only necessary permissions
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO dalt_user;
-- Don't grant DROP, CREATE, or superuser4. Disable Debug in Production
APP_DEBUG=false5. Use Environment-Specific Files
.env.local
.env.staging
.env.productionLoad appropriate file based on environment.
Next Steps
- CLI Commands - Use configuration with commands
- Deployment Guide - Production configuration
- Working with Database - Database setup