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.
Related
I'm a long time Zend Framework user (now renamed Laminas). But I decided to give a try to last Symfony version. So I just installed it in 5.1.2.
I'm facing a question regarding the multiple environments deployments. In my compay, we have :
Local environment which is developer pc.
Development.
Staging.
Production.
In ZF-Laminas, we have a global.php file which is located in config directory.
For those of you who are not familiar with this framework, you can override key set in global.php file by creating local.php file.
In this global file, I use to put standard configuration for my application.
For example (prod) :
'open_id' => [
'client_id' => 1234
]
Then, I have development and staging files which car override those values for every environmenet. During the deployment, the file corresponding to the environment is copied to local.php.
Let's say staging.local.php.dist becomes local.php with :
'open_id' => [
'client_id' => 5678
]
Which is fine because value is overriding the one from global file.
I would like the same behavior in Symfony but I don't see something similar in Symfony 5.
So far, I only found two possibilities
Create a bundle which will allow me to have a <bundle_name>.yaml file in config/packages directory. According to the documentation (https://symfony.com/doc/current/configuration.html#configuration-files), I will be able to have dev, prod and staging overrides. But it forces me to create a bundle to handle just some standard configurations, which is huge.
Use .env files. But .env files only allow string data, not complex data like arrays.
What do I miss ? Or is it my "zend" way of doing things that is wrong ?
Thanks.
You can also create services_%env%.yaml (services_dev.yaml, services_test.yaml) files for each environment. It will allow you to define different parameters and override/define services for each environment.
Example:
config/services_dev.yaml
parameters:
hello: 'world'
From what I understand from your post, your goal is to have different config values based on the server you are on. If this is the case, you can use environment variables (in the .env file or .env.local for server specific config). You can then use these values in your applications by binding the env var to a parameter. This parameter will then be available within the configuration by using %parameter_name% as value or within the container. You can also pas parameters to services (service definitions are handled the same way as any other config). For more information you can checkout these sources:
https://symfony.com/doc/current/configuration/env_var_processors.html
https://symfony.com/doc/current/configuration.html
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.
My project is two Laravel applications in one folder. Why two separate applications instead of one? I have good reasons. Anyway, for example, every time I want to change the app name in the config (config/app.php) I go and edit the .env file in both directories (of the two applications) and then execute artisan config:cache to update the cached config.
It's not much of a task or anything, but it's a bad design I believe. So, I am looking for a way to take the values shared between these two application out to a separate file. And have these application load this file and override/apply the values inside it to the app configuration.
So, now I have a config.php in the root directory (which holds the other two apps in /app1 and /app2). And inside each app's AppServiceProvider I call the config.php and loop through the values and set each using Config::set(..., ...).
This worked for me well, but of course changing the values loaded in the \Config package doesn't change the values that were fetched from it earlier to this point. For example app()->environment() returns the value set in the config/app.php, not the new value introduced in the global config.php. That's because the App library asked for the env property before I could override it!
So, what I want your help with is: you either tell me about a more smart/standard way to achieve 1 config for 2 apps setup. Or tell me where to put the code that overrides the app config with the new values from my global config.php (currently I put the code in register() in App\Providers\AppServiceProvider)
You can share env settings by removing one of .env files and make a symlink to the other file instead of a common file. In this case if you change a file, both applications get changed values.
In Laravel 4, you could set an environment based config folder structure:
/config/app.php
/config/dev/app.php
/config/staging/app.php
/config/testing/app.php
Can you do this with Laravel 5? I understand the .env concept and I'm using that to define which environment I'm in. But I need to define a config value that is an array of arbitrary length, and you can't do that with .env files.
An example of what I'm trying to achieve:
if (in_array($request->input('value'), config('app.valid_values')) {
// do something
}
This valid_values is simply an array of values. It's of arbitrary length, so you can't just set them in your .env file like:
VALID_VALUE1=...
VALID_VALUE2=...
etc.
AND the array needs to be different for each environment.
This was easy to do in Laravel 4 with environment configuration folders. But how do you do this with Laravel 5?
If you need to create an array on values, you can create on string format and when you need you can parse them
MY_ARRAY_VALUE=1,2,house,cat,34234
When you need them
$myArrayValue = explode(',', env('MY_ARRAY_VALUE'));
Or save your values in JSON and get them with json_decode()
$myArrayValue = json_decode(env('MY_ARRAY_VALUE'), true);
Extra info:
On Laravel 5, you need to translate all your configs files in one .env file.
On each environment your .env file will be diferent with values for this environment.
To set your environment, you need to change the value of APP_ENV in your .env file
APP_ENV=local
And you can create your own variables in that file
https://laravel.com/docs/5.2/configuration#environment-configuration
This is an extract of the upgrade guide to Laravel 5.0
https://laravel.com/docs/5.2/releases#laravel-5.0
Instead of a variety of confusing, nested environment configuration directories, Laravel 5 now utilizes DotEnv by Vance Lucas. This library provides a super simple way to manage your environment configuration, and makes environment detection in Laravel 5 a breeze. For more details, check out the full configuration documentation.
You can find a default .env file here: https://github.com/laravel/laravel/blob/master/.env.example
It is often helpful to have different configuration values based on the environment the application is running in. For example, you may wish to use a different cache driver locally than you do on your production server. It's easy using environment based configuration.
To make this a cinch, Laravel utilizes the DotEnv PHP library by Vance Lucas. In a fresh Laravel installation, the root directory of your application will contain a .env.example file. If you install Laravel via Composer, this file will automatically be renamed to .env. Otherwise, you should rename the file manually.
Generally for Phpdotenv
Phpdotenv is about storing values in environment, not general purpose config library. Environment is UNIX concept and the values are always interpreted as character strings. Converting to different datatypes such as arrays or booleans even though convenient would be outside the scope of this class.
Laravel config system
Laravel's config system is already separated. phpdotenv does environment, laravel does config. Then once config is done, environment is ignored. The concern of parsing environment variables from strings into whatever is passed on to laravel (weather that be their env function, or exploding inside your config files).
Good practice
In other words, use Config::get() to get a specific conf file with your desired structure and you have what you need.
You should never use env() in the code directly when it is outside of the config folder according to the Laravel guidelines. It's a good practice to use config(). In config files use env() to get the data from .env file.
Lumen's docs state that "Lumen supports several broadcast drivers out of the box: Pusher, Redis, and a log driver for local development and debugging. A configuration example is included for each of these drivers. The BROADCAST_DRIVER configuration option may be used to set the default driver."
In my .env file I have set BROADCAST_DRIVER=pusher. Where/how can I configure my pusher ID, key, and secret? I see that in Laravel a configuration file for setting these options lies within config/broadcasting.php. Where can I set these options within Lumen?
Temporarily I have edited Illuminate\Broadcasting\BroadcastManager and hard coded my values in.
protected function createPusherDriver(array $config)
{
// override
$app_id = 'hidden';
$key = 'hidden';
$secret = 'hidden';
return new PusherBroadcaster(
new Pusher($key, $secret, $app_id, Arr::get($config, 'options', []))
);
}
Okay, I figured it out. Essentially you have to add in the config files yourself.
Create a config directory in the root of your app.
Copy config/broadcasting.php from a working laravel installation into this directory
Add the following keys to your .env file: PUSHER_SECRET, PUSHER_KEY, PUSHER_APP_ID
In general, Lumen supports two modes of configuration:
Setting environment variables consumed by Lumen's internal config files
Creating project config files that override Lumen's internal configuration
Lumen provides environment-based configuration variables needed to configure most of the framework's components, including Pusher. Though unclear from the docs, we can also configure Lumen through config files like Laravel. This enables advanced configuration that Lumen may not support through its built-in configuration structure.
By default, new Lumen projects don't provide configuration files like new Laravel projects do in the config/ directory. As #Feek discovered, we can create the config/ directory and add any needed config files. For example, we can create the config/broadcasting.php file to set up broadcast connections.
When creating a config file in the project like this, Lumen will automatically read configuration values from the file if it matches the name of one of the Lumen built-in config files. If we want to add a custom configuration file that doesn't match one of Lumen's internal config filenames, we need to manually instruct Lumen to read the config file in a service provider or in bootstrap/app.php.
For instance, to load configuration values from config/my-custom-config.php, add this line to the application's boot process:
$app->configure('my-custom-config');
In the particular case of this question, Lumen's built-in broadcasting.php config file reads Pusher environment variables for us, so we don't need to create a config file in the project for these. Simply set the BROADCAST_DRIVER, PUSHER_SECRET, PUSHER_KEY, and PUSHER_APP_ID in .env.