Laravel env vars in bootstrap/app.php? - laravel

I put this in bootstrap/app.php
dump(env('STRIPE_SECRET_KEY'));
and it's coming back null even though this environment variable is set in my .env file.
I suspect that file hasn't loaded by the time bootstrap/app.php runs.
What file does run after the env files are loaded? Is there another startup file where I can put this sort of thing?

1)
You can use env() only in config files config/*.php but there are some trick. Add this code at the beginning of bootstrap/app.php:
try
{
(new Dotenv\Dotenv(__DIR__.'/../'))->load();
}
catch (Dotenv\Exception\InvalidPathException $ignored) { }
Look here.
2) You can do it in AppServiceProvider file.

Related

Laravel Localization does not return translated string

I can't seem to figure out what I'm doing wrong.
I want my application to use "np" as its default locale. So I change the locale key in config/app.php to 'np.' And when I check for the current location in my controller, it returns 'np', so it's working fine until here. Then I created a "np.json" file directly inside the lang directory, which has the following content:
{
"Candidate": "उम्मेदवार"
}
Now when I try to return the translated string using:
__('Candidate')
It returns "Candidate" instead of "उम्मेदवार", even if the current locale function still returns "np". So I ran the following commands trying to clear the cache.
php artisan optimize:clear
php artisan config:clear
php artisan cache:clear
But still, the issue persists.
You have to call the translations like <filename>.translation_string, and that file has be placed inside a folder with your language code.
In your case, create a folder inside lang called np, then place a translation file (example: mystrings.php) in it, so you have this file in a folder lang/np/mystrings.php.
Your trans file looks like
<?php
return [
'Candidate' => 'उम्मेदवार',
];
Now, you have to set the lang to your language np with app()->setLocale('np'); or you set it directly in your .env and now you can call
echo __('mystrings.Candidate');
to get your translation.
Hope it helps

How to make Laravel read .env file again?

I'm changing Laravel .env variable after a process, and checking that variable everytime, if it's true code doing one thing, if it's false another thing. But after changing the .env variable I have to restart with php artisan serve, I don't want to do this. Why Laravel does not read the new env variable, it changes on the .env file itself.
You shouldn't have to manipulate environment variables during runtime. If you're trying to manipulate an environment variable, you should probably instead utilize a config value. These configuration files are placed in the config/ directory of your project.
Then during runtime, you can use the following built-in config helper to get/set your config variable.
$value = config('app.timezone');
// To set configuration values at runtime, pass an array to the config helper
config(['app.timezone' => 'America/Chicago']);
$newValue = config('app.timezone');
You can read more about this process here (you can change the version to your version):
https://laravel.com/docs/5.5/configuration#accessing-configuration-values

Laravel - How to declare a file path constent in .env file?

In one of my Laravel based application, I want to include a JSON key file that is currently located in public/key/store.json
Now I want to write a constant in Laravel .env file so that I can access that file anytime I want. So, I write the following line of code:
KEY_FILE='/public/key/store.json'
But it show file path does not exist.
Can anyone tell me what's wrong in my declaration?
you should push your path in your app/config.php not in your .env

Use multiple env files

I'm wondering if there's a way in Laravel to specify a set of env files to load. My exact problem is I want to add something like a suffix to all my .js and .css resources. Ideally I'd have a suffix like the release date because it would be ok for these files to be cached within the same release but I would like the caches to be invalidated on the next release. However I want to avoid reading, modifying and saving the .env file if possible and would instead prefer to create a new file e.g. .env.rdate which would be generated via a script, e.g.
echo APP_RELEASE_DATE=`date +%s` > env.rdate
or something like this. Is this at all possible or do I have to read/update/write the .env file instead?
Create your .env.rdate file next to .env file.
Put this to your AppServiceProvider boot method:
$dotenv = new \Dotenv\Dotenv(base_path(),'.env.rdate');
$dotenv->overload();
After you can use in your project:
ENV('APP_RELEASE_DATE')

Laravel 5 doesn't read values from dot ENV files

I don't know if this question is relevant or not. LARAVEL 5 is still in developmental phase. I have pulled LARAVEL 5 after watching one of the Laracast video about new features in LARAVEL 5. I couldn't resist to wait for its formal release.
I named the local environment dot file as .env.local.php. But for some reason I am unable to get the the values from this dot file when using $_ENV['KEY'].
I am quite sure that I have configured the environment correctly. When doing $app->environment() shows the correct environment. Has it been changed in LARAVEL 5 the way we get the values from dot files or am I missing something ?
By default in environment.php file you have something like that:
if (file_exists(__DIR__.'/../.env'))
{
Dotenv::load(__DIR__.'/../');
}
so only .env file is being read (notice .env not .env.php - so you should rename your file - or you can add as 2nd parameter file name .env.php if you want). Any other environment files (.local.env) are not being read by default - you will need to load them manually.
If you don't have such code by default, you should probably update/install Laravel 5 again (changes appear very often)
Now, I don't know what method you use, but you can put in your .env file also your environment name in for example APP_ENV variable, create .local.env file with content you want and then you could use in environment.php file:
if (file_exists(__DIR__.'/../.env'))
{
Dotenv::load(__DIR__.'/../');
if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) {
echo "loading";
Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');
}
}
If you don't want to do it this way, you can probably change the other and load env file you want based on $env assuming you use PC based environment detection.
If it's unclear you can also look at What's the correct way to set ENV variables in Laravel 5?

Resources