How to Change the loaded environement in Laravel - php

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

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 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.

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

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

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).

Trying to Migrate in Development Mode

I'm trying to figure out how I can get my environment set to development mode inside my Laravel application.
bootstrap/start.php
$env = $app->detectEnvironment(array(
'development' => array('blog.app'),
'production' => array('')
));
Inside my routes file I have the following:
Route::get('/', function()
{
dd(App::environment());
});
When I go to blog.app it says its set to production and I'm not sure why. The purpose of figuring out why its giving me production instead of development is because I am attempt to run the command "php artisan migrate" and its giving me the following.
My-iMac:blog my$ php artisan migrate
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command?
So I need help understanding why its still saying application is in production mode when I'm attempting to be in development mode.
Go inside your project directory. Then navigate to bootstrap/start.php.
Edit the local array specified in $app->detectEnvironment with your machine name.
Laravel will automatically check you machine name every time and know that it is a local environment and use the local configuration files instead. To use the local config files you can navigate to app/config/local and edit the files there. The files in app/config belong to the production environment. You can copy these files in the local folder and then edit them accordingly, for example you database configuration probably will differ on your local machine and the server.
You can simply edit the files in the config folders and Laravel will detect automatically the environment according to the array in the start.php and use the config files related to the machine on which you are working. You can also specify more environments and create their entries in the start.php file and then go and create a folder by the same name in the app/config folder and create config files for that environment in that folder.
Example: If you want to have two local environments you can edit you start.php file like so
$env = $app->detectEnvironment(array(
'local' => array('Machine1', 'Machine2'),.
'local2' => array('Machine3', 'Machine4'),
));
And then navigate to the app/config folder and create two folders like local and local2 and have separate files for each of them. Now the machines with names Machine1 and Machine2 will automatically use config files present in local folder and Machine3 and Machine4 will use config files in local2 folder. This way Laravel provides great flexibility in setting environments.
For more information check out the Environments and Configuration Episode on Laracasts.
You need to figure out your machine name. In order to do that, run this code anywhere:
echo gethostname();
exit;
More info about the function: http://php.net/manual/en/function.gethostname.php
It will output your machine name, then you need to paste it in the 'development' array.

Categories