Laravel set environment variable - laravel

Trying to set the environment variable from commandline like
php artisan serve --env=someenv
But a var_dump(App::environment()) outputs string 'production' (length=10).
Shouldn't it be someenv ?

This environment you set on php artisan seve is just for that particular command, running on cli.
Note that this is not you application running, just a webserver, provided by PHP so you don't need to install a full apache or nginx to test your application.
Your web application will run under a different environment and you still need to provide a correct environment adding it to your bootstrap/start.php file:
$env = $app->detectEnvironment(array(
'local' => array('localhost', '127.0.0.1', 'example.com'),
));

Laravel 5 can use a .env file in your installation base directory to determine your environment. For example, to set the current environment as the „local“ one, create a file .env with
APP_ENV = local
Please see another answer for more details on this mechanism.

Related

Why "App::environment()" and "php artisan env" returns different results?

This is the env variable in my config/app.php
'env' => env('APP_ENV', 'production'),
I changed the APP_ENV variable to staging from .env like the following:
APP_ENV=staging
I run php artisan config:clear.
After that, php artisan env returned:
Current application environment: staging
However, the App::environment() still returns local.
Route::get('/', function () {
dd(App::environment());
});
Any ideas? Did I miss something?
It seems, on the application level, the environment variable is set correctly. The value should be staging.
However, it can be overridden by your web server settings.
The current application environment detection can be overridden by
defining a server-level APP_ENV environment variable.
Source: Laravel 9 Determining The Current Environment
You could check the web server settings.
E.g. with Apache server, the environment variable could be set, in files like httpd.conf as following:
SetEnv ENVIRONMENT "local"

Add Laravel .env variable to Vue component

I would like to get access to an .env variable using Vue JS.
In my .env file I have added the 'MIX_' prefix to the var.
MIX_VAR=key
And then in the vue component, I have in the created():
console.log(process.env.MIX_VAR);
I keep getting undefined as the result.
I have tried clearing config cache, but still getting the same issue. Any ideas?
in windows :
thats worked for me without any require in webpack.mix
... just add a new variable in env file with this prefix : MIX_
MIX_API_URL=http://laravel:8000
but need to restart php artisan serve and also restart npm run watch....
let api_url = process.env.MIX_API_URL;
console.log("my env variable:");
console.log(api_url);
in linux or docker:
i didnt use them yet
You must build your JS for the env variables to be replaced. You can do this with npm or yarn
https://laravel.com/docs/5.7/mix#running-mix
Pulled from the official docs # https://laravel.com/docs/5.6/mix#environment-variables
Environment Variables
You may inject environment variables into Mix by prefixing a key in your .env file with MIX_:
MIX_SENTRY_DSN_PUBLIC=http://example.com
After the variable has been defined in your .env file, you may access via the process.env object. If the value changes while you are running a watch task, you will need to restart the task:
process.env.MIX_SENTRY_DSN_PUBLIC
The most important thing to remember is that you have to use Laravel Mix for this to work. Mix is what is injecting the environment variable.
process.env.MIX_VAR / process.env.MIX_STRIPE_KEY
It's will work without any settings. Just run this command
npm run prod
This works for me
https://laravel.com/docs/8.x/mix#environment-variables
However, you will need to restart the task if the environment variable's value changes while the task is running:
e.g If Watch is running then re-run it.
npm run watch

getenv not working in EC2 Server

I'm trying to deploy to production my app that is working well in local.
Thing is when try:
dd(getenv('APP_ENV'));
it returns "false"
but when I connect ssh and type:
php artisan env
I get
production
Any idea why it stopped working???
For the record, In production, my deploy script execute 3 commands:
composer dump-autoload -o
php artisan route:cache
php artisan config:cache
I mention it because it is possibly the only software config that is different.
EDIT: I identify that the problematic command is:
php artisan config:cache
If if do:
php artisan config:clear
problem is solved.
Tx!
When using a cached config the .env file is not used anymore so getenv is useless, because the config is loaded from:
bootstrap/cache/config.php
Instead you can get the current environment from the loaded application configuration like so:
config('app.env');
Or directly using the app helper function:
app('env');
As a third option you can always use the environment method to get the current environment:
app()->environment(); // or App::environment()
Laravel uses the dotenv library internally to load the configuration keys from the .env file and add them to the environment variables using putenv, but when you cache your configuration that loading part is not done anymore because Laravel detects that there is a cache file present and uses that instead, so those keys from .env are not loaded into the environment, this not accessible via getenv.
And because configuration values from the .env file are only cached when they are used in an actual configuration file from the config directory, you need to create a configuration option for them to be cached and accessible when you're using the cache.
So if you want to have a BASE_URL key in your .env file with this value:
BASE_URL=http://domain.com/
If you want to be able to access its value when the configuration is cached, you need to use it in a configuration file. For example, you can add it to your config/app.php file like so:
'base_url' => env('BASE_URL')
Then you can access even when the configuration iit using:
config('app.base_url')
You can read more about accessing configuration values in the Laravel Documentation.

Laravel can't detect environment variables set by Forge when run from console

For instance, if I set the environment variable DB_HOST with value localhost through Forge, it creates an entry in the nginx file of fastcgi_param DB_HOST "localhost";
When I enter the console and try to run any command, any command that depends on that environment variable returns false if I do getenv('DB_HOST').
However, if I run the site from the browser, there is no problem. Only the console app seems to have this issue.
What is erasing the contents of the environment variables, or preventing them from being read, when in the console?
When you're running from the command line, no environment is set as those are for the HTTPD. Console requests do not go through nginx, just PHP.
You can set the environment for the command you're running, with the --env option. Example:
php artisan migrate --env=develop
I could not find a way to keep the environment variables in one place (the nginx config file) but there is a workaround of creating a .env.php file in your app root dir, which makes them available for production.
To do this, I added the following to my .env.php file:
return [
'DB_HOST' => 'localhost',
'DB_NAME' => 'thename',
'DB_USER' => 'theuser',
'DB_PASS' => 'thepass'
];

laravel 4 on beanstalk, php artisan how to figure out the environment

I have 3 environments
development
staging
production
When I use amazon beanstalk every request goes through a load balancer from which point you end up on an ec2 server which could be different every time (hence the problem of figuring out the environment, I can't use the machine name as it's different every time).
I found out about environment variables in beanstalk configuration so I pass an environment variable PARAM_1 from .ebextensions.myapp.config that essentially dictates the environment to use with the following trick.
$env = $app->detectEnvironment(array(
'staging' => $_SERVER['PARAM_1'] == 'staging' ? array(gethostname()) : array('not-staging'),
'production' => $_SERVER['PARAM_1'] == 'production' ? array(gethostname()) : array('not-production'),
'development' => array('mylocalname')
));
This works fine except for php artisan commands! For some reason when running the artisan commands I can not access this environment variable so I'm stuck!
I would like to run php artisan migrate but how do I tell it the environment it's in! (based on the environment I have different database configs)
You can define the environment like this:
php artisan migrate --env=Development
You can also use another SO answer to hard code the artisan enviornment into your code: Environment driven database settings in Laravel?

Resources