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.
Related
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
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.
I am new in laravel and I found two file with name database.php
First:
app/config/local/database.php
Second:
app/config/database.php
What is the different between usage of these files?
Laravel 4 uses config/ENVIRONMENT/* to allow developers to specify configurations for a given environment. If a configuration file isn't found in the config/ENVIRONMENT directory it will use the default configuration in config/.
Laravel 5 uses a .env configuration file to easily add environment variables. You simply tell your configuration file config/database.php to retrieve these variables dynamically.
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.
My laravel 4 project has multiple environments set up.
The environments setup are: development is called 'development' and production is called 'production'.
In my app/config folder, I have two extra folders. One for each name of the environment.
See screenshot:
Inside these folders I have separate config files for database.php and app.php.
These are working as expected. Laravel uses my local database configuration when running locally and production when running live.
Now, I've added a new package which requires a config file. This resides in /app/config/packages/greggilbert/config.php.
I need to be able to set different config files the same way I do for the database. So, I assumed creating the same file structure in /development or /production would work, but this does not seem to be the case.
Here is a new screenshot:
Package-related, environment-specific configuration files have to be placed in the package's directory within a subdirectory named after the environment. So in your case this would be
app
- config
- packages
- greggilbert
- recaptcha
- development
- config.php
- production
- config.php
But it can get really ugly and cumbersome in some cases. This can be solved in a (in my opinion) cleaner and more elegant way through enviroment files in your application root directory:
/.env.development.php
<?php
return [
// any configuration settings for your local environment
'RECAPTCHA_TEMPLATE' => 'customCaptcha',
'RECAPTCHA_LANGUAGE' => 'en',
];
That way, you could leave the package directory as it is:
app
- config
- packages
- greggilbert
- recaptcha
- config.php
Laravel will load the environment file according to the environment and make the environment variables globally available through getenv(). So now you could just grab the specified template for the current environment with getenv('RECAPTCHA_TEMPLATE') from the config.php.