Cấu hình
File Environment
Framework sử dụng file .env để lưu trữ các biến môi trường.
# Application
APP_NAME=MyApp
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost:9501
# Server
SERVER_HOST=0.0.0.0
SERVER_PORT=9501
SERVER_WORKER_NUM=4
# Database
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=root
DB_PASSWORD=
# Redis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=null
# JWT
JWT_SECRET=your-secret-key
JWT_TTL=3600
# Mail
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls
# RabbitMQ
RABBITMQ_HOST=127.0.0.1
RABBITMQ_PORT=5672
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
Truy cập Environment Variables
Sử dụng hàm env()
// Lấy giá trị
$debug = env('APP_DEBUG');
// Khi không có gía trị lấy giá trị mặc định
$port = env('SERVER_PORT', 9501);
Sử dụng Config
use Vietiso\Core\Config\Config;
// Lấy config
$database = Config::get('database.default');
// Với giá trị mặc định
$timezone = Config::get('app.timezone', 'UTC');
File cấu hình
Các file cấu hình được đặt trong thư mục config/:
config/
├── app.php # Cấu hình ứng dụng
├── database.php # Cấu hình database
├── cache.php # Cấu hình cache
├── mail.php # Cấu hình email
├── queue.php # Cấu hình message queue
└── cors.php # Cấu hình CORS
Ví dụ config/database.php
<?php
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 3306),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
],
],
],
];
Ví dụ config/app.php
<?php
return [
'name' => env('APP_NAME', 'Vietiso'),
'env' => env('APP_ENV', 'production'),
'debug' => env('APP_DEBUG', false),
'url' => env('APP_URL', 'http://localhost'),
'timezone' => 'Asia/Ho_Chi_Minh',
'locale' => 'vi',
];
Cấu hình Server
Worker Configuration
# Số worker processes
SERVER_WORKER_NUM=4
# Số task worker
SERVER_TASK_WORKER_NUM=2
# Max connections
SERVER_MAX_CONNECTION=10000
Hot Reload
Trong development, bật hot reload để tự động restart khi code thay đổi:
composer start:watch
Cấu hình Connection Pool
// config/database.php
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => 60.0,
],
Cấu hình theo môi trường
Tạo các file .env riêng cho từng môi trường:
.env.local
.env.development
.env.staging
.env.production
Chỉ định môi trường khi khởi động:
APP_ENV=production php vietiso start:serve