I need environment specific config file which will not be included in the version control, file with php array seems to be a good choice especially considering that I can define values in desired type and will not need to convert them, but a lot of frameworks and libs use .env files. What are the advantages of .env files and why one should be using them?
I like the way Laravel do it. It implements the config file and env file.
in config/*.php, you'll define things like:
<?php
// config/app.php
return array(
'myconfig' => env('MYCONFIG', 'default')
)
in .env file
MYCONFIG=something
so you'll only need to use config function everywhere.
config('app.myconfig')
btw, it's easy to implement both (isolated or together).
.env files are a standard. It is simple to use and as it sounds like, it is environment independant.
You should definitively use this solution
Related
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.
In Laravel 4's cascading configuration files I was able to reference one configuration key from another. For example, I liked to have app.debug set to true or false, and then I could switch error reporting to Sentry to the opposite of that in a different configuration file, by setting the relevant setting to !Config::get('app.debug').
I knew this was a little hairy because presumably whether the config value can be found depends on the order in which files are loaded. But I managed to get it working each time.
I'm having no such luck in Laravel 5. I get a "class not found" error when I try to do Config::get(...) in a configuration file. Using the config(...) helper instead produces no error but the values aren't retrieved.
The convention, of course, is to use environment variables, the .env file and env(...) to grab the values. That way each configuration key can use the value of the environment variable. That's fine and works, but I have some values which are shared between environments, and I do want some of these values to be committed to version control.
Using the .env.example file isn't a good solution for me since I don't want to have to remember to copy values over to the .env file of each environment if they change.
How can I store some configuration values in a way where they are shared between environments, are present in version control, and can be referenced in multiple configuration files?
This is my solution, which works, but I'm interested in other possibilities.
Add a new shared file, checked in to VCS, called .env.shared. Its structure is just like .env. Edit bootstrap/app.php to load in these variables (in addition to .env):
// Load shared environment variables
$app->afterLoadingEnvironment(function () use ($app) {
Dotenv::load($app->basePath(), '.env.shared');
});
I added this right after $app is instantiated.
I have the Laravel Administrator Plugin and I set up a setting administrator page that is located in:
app/config/administrator/settings/site.php
The application can store the data, but when i try to get some data in one of my controllers like this:
Config::get('administrator.settings.site')
I can get a returned value... Always null or array 0.
How I can get those configuration values?
Solution:
You can use a file path rather than dot notation for file paths:
Config::get('administrator/settings/site.variable_name');
Some Explanation
Dot notation is used to get variables inside the config array within a file, rather than to denote file paths.
Therefore you can use Config::get('administrator/settings/site.variable_name'); to get $variable_name from administrator/settings/site.php, or ENV_DIR_NAME/administrator/settings/site.php depending on your environment.
Directories within in app/config are read as environments. For example app/development/database.php will work with Config::get('database.whatever') when you're in your "development" environment while app/production/database.php will be read with Config::get('database.whatever') when you're in the "production" environment.
Laravel falls back to the config/config/*.php file when no environment-specific file is present.
Note
I believe that admin package has a config file in app/config/packages/frozennode/administrator/administrator.php which the package would like you to use for it's settings. That should be available using this convention: Config::get('package::file.option');
I ran into a very similar problem when using this Laravel-Excel package.
The solution I needed was slightly different to the solution above. I needed to take advantage of Laravel's config overriding feature to have one config setting for normal execution and a different one for testing. Specifically, I normally wanted Excel files to be stored in storage/excel and I wanted them to be put in storage/testing/excel under testing.
The technique of using a slashed path didn't work because it points to a specific file so didn't respect the different environments:
$directory = Config::get('packages/maatwebsite/excel/export.store.path');
The solution was to use a package prefix so that the config loader would respect the environment:
$directory = Config::get('excel::export.store.path');
I'm not exactly sure where the excel shorthand name comes from but I suspect it's something to do with the Excel facade in the package exposing itself as 'excel'.
I am trying to "include" a php script into one of my views (landing.blade.php).
The script is in:
/laravel-master/public/assets/scripts/config.php
When I try to include this code in the view:
<?php include_once('/assets/scripts/config.php'); ?>
I get the error: include_once(/assets/scripts/config.php): failed to open stream: No such file or directory
This is on localhost using MAMP. I'm not sure if there is a different set of rules I need to use with Laravel 4 to include a php file. Thank you for your help!
First, it's not really recommended that you keep your PHP files in the public directory, they should be kept inside the app folder. I'd suggest you create a folder inside app, something like includes and put your files there. Then, you include it, do:
include(app_path().'/includes/config.php');
Although, since it looks like you're trying to load some configuration files, I'd recommend you also check out Laravel's own way of handling configurations. For instance, if you created a myapp.php file inside the app/config folder, Laravel would handle it automatically for you, as long as you'd have some key-value pairs, like this:
<?php
return [
'name' => 'Raphael',
'gorgeous' => true
];
You could then retrieve these values using the Config class:
Config::get('myapp.name'); // Raphael
This is a better solution because you can also take advantage of Laravel's environment configuration.
You can use includes in HTML forget about concatenation
#include('foldername.filename')
#include('filename')
This is another way:
require 'file path';
Basically, I have a file called Constants.php, which is located in /app/Lib/Constants.php, and I want to use it in one of my Model classes (and possibly other classes that I've built). However, I can't seem to figure out the correct syntax to do this (and CakePHP's documentation on App::import() is hard for me to understand).
How do I properly do this? Or is there another better convention of defining user-defined constants in CakePHP applications (besides using the Configure class)?
If the Constants.php file is one in which you are defining configuration variables for your application, I would suggest using the Configure class.
Place the Constants.php file into the Config/ directory of your application.
In your Config/bootstrap.php you load the constants with the following line:
Configure::load('Constants');
Now, anywhere in your application you can read configuration information with the following:
Configure::read('Constants.Something');
Note, the Configure setup requires a particular format for your configuration variables. Use the following in your Constants.php file:
$config = array('Constants' => array(
'Something' => 1234,
'Foo' => 'Bar',
));
This is the recommended way to do configuration information, loaded in and made available to your entire application.
The manual is a better place to look as it explains it better :) All you have to do is this:
App::import('Lib', 'Constants');