I am developing an app using Laravel + VueJS + Homestead and as everybody knows on Laravel 5.2 we have a env file where we can set env variables... I would like to do something like that but in way where I can access it from my javascript code!
I have read about a proccess.NODE_ENV but I don't know if I got it right but it looks like works only on npm start no? As I am running my app through homestead I don't really know how to do it!
Thanks in advance!
The “.env” file Starting from v1.28 , the .env file is placed at the base of the project directory. Project directory can be explicitly defined with the --file option or COMPOSE_FILE environment variable. Otherwise, it is the current working directory where the docker compose command is executed ( v1.28 ).
I have a config.js
file that I keep config variables in, like:
const IS_LOCAL = process.env.NODE_ENV !== 'production'
const API_BASE_URL = IS_LOCAL
? 'http://api.domain.dev/v1'
: 'http://api.domain.com/v1'
const LOGIN_URL = "auth/login"
const LOGOUT_URL = "auth/logout"
const REFRESH_URL = "auth/refresh"
export default {
IS_LOCAL,
API_BASE_URL,
LOGIN_URL,
LOGOUT_URL,
REFRESH_URL
}
Then if I need a config variable in a file I just call:
import {REFRESH_URL, LOGIN_URL} from "./config.js"
You could dump the APP_ENV
environment variable to the page hosting your JavaScript and later access it or pass it to Vue.
<script type="text/javascript">
var env = "{{ env }}";
</script>
and then in your controller...
$env = getenv("APP_ENV");
would get you the value of APP_ENV
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With