How to set .env values in laravel programmatically on the fly

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Programmatically Handling .env Values in Laravel: A Comprehensive Guide for Enhancing Custom CMS Flexibility

As a senior developer working on a custom CMS, you may want to provide your users with complete flexibility and control over their application's configuration. This includes the ability to set up their .env values from within the controller, offering them the option to modify these settings without modifying the actual file. This blog post will offer a thorough answer, best practices, and a step-by-step guide on how to achieve this functionality in Laravel. Before we dive into the details, let's briefly discuss the importance of .env files and their role in modern PHP applications.

Understanding .env Files and Their Role

.env files are crucial for storing configuration data, such as database credentials, email settings, and other key information required by your application. By maintaining these details within the .env file, you can easily manage and maintain different configurations for various environments (e.g., development, testing, or production). This practice greatly simplifies deployment processes and ensures that each environment has its unique configuration.

Writing .env Values Programmatically

To set up the data received from your users, you can use a combination of Laravel's Artisan commands and PHP. Here's an outline of the process: 1. Collect input data from user through the GUI or API. 2. Validate and sanitize the received data. 3. Parse the .env file to extract current values (if any). 4. Update the .env file with new values, making sure to preserve existing configuration settings. 5. Save and overwrite the modified .env file. 6. Test and verify that your custom CMS loads the updated configuration correctly.

Code Examples and Best Practices

Below is a basic code snippet to demonstrate how you can handle .env values programmatically within your Laravel application. The example shows how you can set up database credentials:

// Collect input data from user
$input = \Request::all();
$database_host = $input['db_host'];
$database_username = $input['db_user'];
$database_password = $input['db_pass'];
$database_name = $input['db_name'];

// Sanitize and validate received input data
if (!filter_var($database_host, FILTER_VALIDATE_IP)) {
    return redirect()->back()->withErrors(['Invalid database host. Please try again']);
}
if (empty($database_username) || empty($database_password) || empty($database_name)) {
    return redirect()->back()->withErrors(['All fields are required. Please fill in the necessary details']);
}
// Parse .env file and extract current values
$current_env = parse_ini_file(base_path() . DIRECTORY_SEPARATOR . '.env');
if (!isset($current_env['DB_HOST']) || !isset($current_env['DB_USER']) || !isset($current_env['DB_PASSWORD']) || !isset($current_env['DB_DATABASE'])) {
    return redirect()->back()->withErrors(['Database configuration not found. Please ensure your existing .env file contains the required settings']);
}
// Update .env file with new values and save it
$new_env = array_replace([
    'DB_HOST' => $database_host,
    'DB_USER' => $database_username,
    'DB_PASSWORD' => $database_password,
    'DB_DATABASE' => $database_name
], $current_env);
file_put_contents(base_path() . DIRECTORY_SEPARATOR . '.env', implode(' ', array_map('trim', array_merge(['# CUSTOM CMS CONFIGURATIONS\r\n'], $new_env))));

// Verify and test the newly updated configuration
\Artisan::call('config:clear');
\Config::set('database.default', 'mysql');
$connection = \Illuminate\Support\Facades\DB::getDoctrineConnection();
$schemaManager = $connection->createSchemaManager();
if (!$schemaManager->tableExists('users')) {
    return redirect()->back()->withErrors(['Unable to connect to the database. Please ensure your custom CMS is configured correctly']);
}

Conclusion

By following these steps and incorporating best practices, you can successfully set up your .env values programmatically in your Laravel application. It's essential to balance the security and flexibility needs of your users with the integrity of your custom CMS configuration. As always, when implementing new functionality, it's crucial to test thoroughly before releasing updates or making them available to all users.