Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ENV type Laravel + VueJS + Homestead

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!

like image 969
Gustavo Bissolli Avatar asked Apr 25 '16 00:04

Gustavo Bissolli


People also ask

Where can I find .env file?

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 ).


2 Answers

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"
like image 196
Jeff Avatar answered Oct 18 '22 21:10

Jeff


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

like image 22
megubyte Avatar answered Oct 18 '22 21:10

megubyte