To handle different configurations for local and production environments without manually toggling values in config/app.php, you can leverage environment variables in your .env file. This way, you can keep your config/app.php file in version control and use different .env files for each environment.
Here's a step-by-step solution:
-
Use Environment Variables in
config/app.php:Modify your
config/app.phpto use environment variables for the URLs. This way, you don't need to comment/uncomment lines based on the environment.'url' => env('APP_URL', 'http://local.injurymf.com'), 'frontend_url' => env('APP_FRONTEND_URL', 'http://localhost:5173'), -
Set Up Different
.envFiles:-
Local Environment: Keep your local
.envfile with local values.APP_URL=http://local.injurymf.com APP_FRONTEND_URL=http://localhost:5173 -
Production Environment: On your production server, create a
.envfile with production values. This file should not be in version control.APP_URL=https://app.injurymf.com APP_FRONTEND_URL=https://fantasymf.com
-
-
Ensure
.envis in.gitignore:Make sure your
.envfile is listed in your.gitignoreso it is not tracked by Git./.env -
Deploying to Cloudways:
When you deploy to Cloudways, the
.envfile on the server will not be overwritten because it is not tracked by Git. You only need to ensure that the correct.envfile is manually placed on the server once. -
Avoid Removing
config/app.phpfrom Version Control:Keep
config/app.phpin version control. The environment-specific values should be managed through the.envfile, not by modifyingconfig/app.phpdirectly.
By following these steps, you can maintain a clean and efficient deployment process without manually toggling configuration values. This approach leverages Laravel's built-in environment configuration capabilities, making it easier to manage different environments.