ApnaPHP

Documentation

Configuration

Learn how to configure your ApnaPHP application. The boilerplate comes with everything pre-configured!

Environment Configuration

The boilerplate includes a .env.example file. Copy it to create your .env:

cp .env.example .env

Application Settings

APP_NAME="My ApnaPHP App"
APP_ENV="development"
APP_DEBUG=true
APP_URL="http://localhost:3000"
APP_TIMEZONE="UTC"

Database Configuration

# MySQL/MariaDB
DB_DRIVER=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=root
DB_PASSWORD=password
DB_CHARSET=utf8mb4

# PostgreSQL
DB_DRIVER=postgresql
DB_HOST=localhost
DB_PORT=5432
DB_DATABASE=my_database
DB_USERNAME=postgres
DB_PASSWORD=password

# SQLite
DB_DRIVER=sqlite
DB_DATABASE=storage/database/database.sqlite

# MongoDB
DB_DRIVER=mongodb
DB_HOST=localhost
DB_PORT=27017
DB_DATABASE=my_database
DB_USERNAME=
DB_PASSWORD=

Configuration Files

App Configuration (config/app.php)

<?php

return [
    'name' => env('APP_NAME', 'ApnaPHP Application'),
    'env' => env('APP_ENV', 'production'),
    'debug' => env('APP_DEBUG', false),
    'url' => env('APP_URL', 'http://localhost'),
    'timezone' => env('APP_TIMEZONE', 'UTC'),

    'providers' => [
        // Add your service providers here
    ],

    'aliases' => [
        // Add your class aliases here
    ],
];

Database Configuration (config/database.php)

<?php

return [
    'default' => env('DB_DRIVER', 'mysql'),

    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', 3306),
            'database' => env('DB_DATABASE', ''),
            'username' => env('DB_USERNAME', ''),
            'password' => env('DB_PASSWORD', ''),
            'charset' => env('DB_CHARSET', 'utf8mb4'),
        ],

        'mariadb' => [
            'driver' => 'mariadb', // Uses MySQL driver (fully compatible)
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', 3306),
            'database' => env('DB_DATABASE', ''),
            'username' => env('DB_USERNAME', ''),
            'password' => env('DB_PASSWORD', ''),
            'charset' => env('DB_CHARSET', 'utf8mb4'),
        ],

        'postgresql' => [
            'driver' => 'postgresql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', 5432),
            'database' => env('DB_DATABASE', ''),
            'username' => env('DB_USERNAME', ''),
            'password' => env('DB_PASSWORD', ''),
        ],

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', 'storage/database/database.sqlite'),
        ],

        'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', 27017),
            'database' => env('DB_DATABASE', ''),
            'username' => env('DB_USERNAME', ''),
            'password' => env('DB_PASSWORD', ''),
        ],
    ],
];

Environment Variables

Application Variables

Variable Description Default
APP_NAME Application name ApnaPHP Application
APP_URL Application URL http://localhost:3000
APP_ENV Environment (development/production) production
APP_DEBUG Enable debug mode false
APP_KEY Application encryption key null

Database Variables

Variable Description Default
DB_DRIVER Database driver mysql
DB_HOST Database host localhost
DB_PORT Database port 3306
DB_DATABASE Database name null
DB_USERNAME Database username null
DB_PASSWORD Database password null

Configuration Helpers

Using the env() Function

// Get environment variable with default
$appName = env('APP_NAME', 'Default App Name');

// Get boolean values
$debug = env('APP_DEBUG', false);

// Get integer values
$port = env('DB_PORT', 3306);

Using the config() Function

// Get configuration value
$appName = config('app.name');
$dbHost = config('database.connections.mysql.host');

// Set configuration value
config(['app.debug' => true]);

Environment-Specific Configuration

Development Environment

APP_ENV=development
APP_DEBUG=true
DB_DATABASE=my_app_dev

Production Environment

APP_ENV=production
APP_DEBUG=false
DB_DATABASE=my_app_prod

Testing Environment

APP_ENV=testing
APP_DEBUG=true
DB_DATABASE=my_app_test

Security Configuration

Generate Application Key

php apna key:generate

CSRF Protection

// In your middleware
use ApnaPHP\Middleware\SecurityMiddleware;

$securityMiddleware = new SecurityMiddleware();

File Upload Security

// Configure file upload limits
config([
    'upload.max_size' => '10M',
    'upload.allowed_types' => ['jpg', 'png', 'pdf'],
    'upload.path' => 'uploads'
]);

Caching Configuration

// config/cache.php
return [
    'default' => env('CACHE_DRIVER', 'file'),
    
    'stores' => [
        'file' => [
            'driver' => 'file',
            'path' => storage_path('cache'),
        ],
        
        'redis' => [
            'driver' => 'redis',
            'host' => env('REDIS_HOST', 'localhost'),
            'port' => env('REDIS_PORT', 6379),
        ],
    ],
];

Logging Configuration

// config/logging.php
return [
    'default' => env('LOG_CHANNEL', 'single'),
    
    'channels' => [
        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/apnaphp.log'),
            'level' => 'debug',
        ],
        
        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/apnaphp.log'),
            'level' => 'debug',
            'days' => 14,
        ],
    ],
];