You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Problem:
When running php artisan key:generate, if the .env file does not contain an APP_KEY entry, the generated key was previously appended to the end of the file. This can make the .env file less organized and harder to read, especially when environment variables are expected to be grouped logically.
Solution:
This update improves the writeNewEnvironmentFileWith method in KeyGenerateCommand.php so that when APP_KEY is missing, it is inserted directly after the APP_ENV line. If APP_ENV is not found, it still falls back to appending at the end. This keeps the .env structure more consistent and readable.
Summary of changes:
When replacing an existing APP_KEY, the behavior is unchanged.
When adding a new APP_KEY, it is now inserted right after APP_ENV if present, otherwise appended at the end.
Benefit:
This change improves the maintainability and readability of the .env file after running the key generation command.
protected function writeNewEnvironmentFileWith($key)
{
$envPath = $this->laravel->environmentFilePath();
$envContent = file_get_contents($envPath);
if (preg_match($this->keyReplacementPattern(), $envContent)) {
// Replace existing APP_KEY
$envContent = preg_replace(
$this->keyReplacementPattern(),
'APP_KEY=' . $key,
$envContent
);
} else {
// Insert APP_KEY after APP_ENV
if (preg_match('/^APP_ENV=.*$/m', $envContent, $matches, PREG_OFFSET_CAPTURE)) {
$pos = $matches[0][1] + strlen($matches[0][0]);
$envContent = substr_replace(
$envContent,
PHP_EOL . 'APP_KEY=' . $key,
$pos,
0
);
} else {
// Fallback: append at end if APP_ENV not found
$envContent .= PHP_EOL . 'APP_KEY=' . $key . PHP_EOL;
}
}
file_put_contents($envPath, $envContent);
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Problem:
When running php artisan key:generate, if the .env file does not contain an APP_KEY entry, the generated key was previously appended to the end of the file. This can make the .env file less organized and harder to read, especially when environment variables are expected to be grouped logically.
Solution:
This update improves the writeNewEnvironmentFileWith method in KeyGenerateCommand.php so that when APP_KEY is missing, it is inserted directly after the APP_ENV line. If APP_ENV is not found, it still falls back to appending at the end. This keeps the .env structure more consistent and readable.
Summary of changes:
When replacing an existing APP_KEY, the behavior is unchanged.
When adding a new APP_KEY, it is now inserted right after APP_ENV if present, otherwise appended at the end.
Benefit:
This change improves the maintainability and readability of the .env file after running the key generation command.
Beta Was this translation helpful? Give feedback.
All reactions