Laravel APP_ENV=local APP_ENV=production difference
How to switch from Laravel local env to production
You have one
.env file, but the contents can differ on each machine you run the application.
Do not forget to run the command
php artisan config:clear after you have made the changes to the .env file. Do this
php artisan env, which will return the correct version and assures you about what it is actually set!
The role of the
.env file is to allow you to have different settings depending on which machine you are running your application. So on your production server, the
.env file settings would be different from your local development environment.
production and local are just environment names that you can use to turn certain testing features on or off in different places.
In development (coding) environment use this settings:
In the production (server) use this settings:
When you are making your application you want to see all errors, but in production you don’t have to show the errors to the users, instead of users can see error 500, and after you can fix it on localhost.
You should always set APP_DEBUG=false when deploying to a live server. Setting debug on might expose database passwords or display your code to users.
APP_DEBUG flag is the difference between showing the application errors (full stack trace) and show a generic "user-friendly" error page.
APP_ENV flag is not a boolean, this is because you can have many environments in your project, for example, production, development, staging, testing, etc. You can "change" the behavior of the app while you are in a specific environment. For example if you are working with PayPal, you don't want to charge real money while you are building the application, then you can use the flag in order to switch between your sandbox and production credentials:
Or another use case as an example is when we are going to run Laravel Dusk only on local and testing environment. In this case, we set the DuskServiceProvider in the boot() method of AppServiceProvider under a certain condition:
In Laravel Blade files you can also put your logics in a
@production // code here@endproduction
// The application is running in "staging"...
// The application is running in "staging" or "production"...
Some more advices:
- use env() only in config files
- use App::environment() for checking the environment (APP_ENV in .env).
- use config(‘app.var’) for all other env variables, ex. config(‘app.debug’)
- create own config files for your own ENV variables. Example:
In your .env put this:
example config app/myconfig.php
'myvalue' => env('MY_VALUE', 'bar'),
// 'bar' is default if MY_VALUE is missing in .env
Access in your code:
config('myconfig.myvalue') // will result in 'foo'
If you execute the
config:cache command during your deployment process, you should be sure that you are only calling the
env function from within your configuration files. Once the configuration has been cached, the
.env file will not be loaded and all calls to the
env function will return
Laravel recommends only to use env() within the config files. Use the config() helper in your code instead of env(). For example you can call config(‘app.env’) in your code.
Every env() call from inside the Laravel application will read the cached config file (that is inside bootstrap/cache/config.php file) and not .env file.
So env() calls read from env file as long as you don’t use php artisan config:cache.
When you use php artisan config:cache all the configuration strings are cached by the framework and any changes you make to your .env file will not be active until you run the php artisan config:cache command again.
These are equal:
.env should not be committed on your repository, like github or bitbucket.
Example in the core of Laravel: fine HandleExceptions:
Why we have an access in config files to the env file. The reason is that we can see the order of loading of these two files in Kernel.php file in this way:
First Environment variables then those variable are ready to use in Configuration.
You see how Laravel put the
production by default environment in