What does the 'env' property in 'app.php' do? - php

The Laravel upgrade guide recommends, when upgrading to 5.2, to add the env property to the app.php configuration file like this:
'env' => env('APP_ENV', 'production'),
What does this do? And how to go about this in the local testing environment?

Laravel is set up to read a .env file for various settings, and one of them is the "environment".
The line you mention is setting 'env' to whatever is in the .env file in the variable 'APP_ENV', or setting it to the default 'production'.
This way, you can just edit the .env file and set the 'APP_ENV' variable to 'testing', 'local', or 'production' and affect changes across the whole application. This is useful because it means you could install your application on different servers and have it feature different configurations based on whether you have set the environment to 'testing', 'local', 'production'.
You might want to read the "Configuration" section here and what it says about setting up the environment: https://laravel.com/docs/5.2/configuration

Related

Is laravel env file more powerful than config files?

The main first file to edit while configuring new Laravel project is usually .env file. It contains all the basic configurations that I need.
However, the config directory contains much more data but some of it is just duplicated.
While some lines in config directory files say "please set it in env file", the documentation says "All of the configuration files for the Laravel framework are stored in the config directory".
Now my question is - what place should contain my configuration information? Is any of these on the top of another? Is any of them more powerful?
.env file for manage your environment such as production, development..
config key firstly loop up env file if key not present, uses default value
Such as:
'name' => env('APP_NAME', 'Laravel'),
.env file should be in your .gitignore file. So in your localhost you can use localhost credentials and in your server you can use production credentials.

How to Change the loaded environement in Laravel

I have to .env files .env.local and .env.pro and I don't know how to switch between files, I tried changing config\app.php from
'env' => env('APP_ENV', 'pro'),
to
'env' => env('APP_ENV', 'local'),
But I get a blank screen with no errors, How to tell the framework which file has to load?
The .env file should not be replicated from your DEV environment to your production environment, so you have different versions of these files in each environment.
https://laravel.com/docs/7.x/configuration#environment-configuration
If you're using git for version control the .env file is excluded by default. However, if you need to give hints to other developers about specific configurations needed you should can put them into the .env.example file. For testing you may as described in the article in the Laravel documentation use a specific .env file, ie.: .env.testing

How to load different mail_driver config setting based on app_env setting in Laravel 5.3 App>

In my PHP Laravel 5.3 app I have my config settings in my .env file with the
APP_ENV=local which can be changed to APP_ENV=production when my app is in production/live mode.
In that .env file I also have a MAIL_DRIVER=preview setting which gets pulled into my config/mail.php config file with env('MAIL_DRIVER', 'smtp') like this:
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
]
So now my question is, when I change my .env setting APP_ENV=local into APP_ENV=production
How can I make it load a different env('MAIL_DRIVER') setting based on that env('APP_ENV') setting?
is there a way to load different .env files for each environment or different config files or how do you handle this in Laravel 5.3.
I remember in older versions of Laravel you simply created a new folder in the config folder for each environment but the whole config system is different from those older versions.
With Laravel 5, you have a different copy of your .env file in each environment.
This file is NOT committed into your repository. Rather, your .env.example file is, and that is what you make a copy of and name as .env in the environment.
In previous versions of Laravel (i.e. <= 4) you could have separate environment files in the same checked out copy of the project and switch between environments, but that doesn't actually make much sense.
Keep your .env.example file up to date with all the options you will need in your app, and initialize them to empty values. When you deploy to a new environment, you copy it as a new file, which keeps you from accidentally committing credentials to your repo and keeps things simple:
cp .env.example .env
Then edit the file and set the values to be appropriate in that specific environment. For example, instead of using test API keys, you may use the production keys for some service. Editing, for example:
sudo vim .env # If you like VIM
or
sudo nano .env # If you like NANO
An exception to what I just said above is with testing. Per the documentation:
You may also create a .env.testing file. This file will override values from the .env file when running PHPUnit tests or executing Artisan commands with the --env=testing option.
In that case though, having your .env.testing file in your repo is most likely acceptable assuming it doesn't contain any sensitive production values, which it probably shouldn't anyway.

Best practice - Distinguish between environments (PHP)

I need to make some slight configuration changes in my local dev env.
Thus, I would like to know what is the "best practice" in distinguishing between environments.
I would like to have only one config file which will behave differently depending on which environment it is executed.
Common and good practice is to use different .env files on different environments.
Basically, a .env file is an easy way to load custom configuration
variables that your application needs without having to modify
.htaccess files or Apache/Nginx virtual hosts. This means you won't
have to edit any files outside the project, and all the environment
variables are always set no matter how you run your project - Apache,
Nginx, CLI, and even PHP 5.4's built-in webserver. It's WAY easier
than all the other ways you know of to set environment variables, and
you're going to love it.
You can use some already created packages to add this to your project. Check this one: https://github.com/vlucas/phpdotenv
Example (just for a demo, not production ready code!):
Note! You will need to add .env to .gitignore file to keep it not synced between environments.
Development .env file:
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=
Production .env file:
DB_HOST=192.168.10.1
DB_USER=dbUser
DB_PASSWORD=123456
Possible config.php file:
<?php
// add composer autoload somewhere here...
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
return [
'db' => [
'host' => getenv('DB_HOST'),
'user' => getenv('DB_USER'),
'pass' => getenv('DB_PASSWORD'),
]
]

Missing the point: Laravel 5 environment setup

So, for my application I see 4 types of environments it can run under:
Local on my machine
Local on my VM where I am using Homestead
Staging
Production
Each environment differs in the database connectivity (type of DB, connectivity to the DB credentials, etc).
In my bootstrap/start.php I have set up this:
$env = $app->detectEnvironment(array(
'local' => array('localhost'),
'localStaging' => array('mylocalStage.dev'),
'staging' => array('myapp-stage.com'),
'production' => array('myapp.com'),
));
How do I enforce those settings? where do I set up that when I'm on 'local' I will use some local settings? Currently when I use
php artisan env
on my root application folder I get:
Current application environment: production
why isn't it picking up I'm 'local'?
After discussing the issue in the comments and in the chat, the conclusion was to use the new .ENV approach which have been introduced in laravel 5.
Which means, you should have an .env file in your root directory with the current environment's settings.
So for local env, your .env file would have different settings than your production's env .env file.
As default you should have a .env.example file, in case you don't have one - you can copy the default content from the git (.env.example#git).
Please notice that when you're updating (pushing) your local version to staging (for instance), that you don't overwrite the .env file. In order to prevent those kind of issues you can use git ignore (so it will skip that file).

Categories