How to get ENV type Laravel + VueJS + Homestead
Asked Answered
R

2

5

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!

Recce answered 25/4, 2016 at 0:18 Comment(2)
If you're using Webpack/Browserify you can do something like var config = require('./path/to/config.json'). #5869716Elemental
Hi @ceejayoz... My main question is how I set which env I am working! Right now my proccess.NODE_ENV is returning undefined and I can figure out how to make it return the right value! What I am doing is something like proposed below... but I don't know how to make the NODE_ENV return the properly value!Recce
M
6

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

Mambo answered 25/4, 2016 at 6:49 Comment(3)
I am not sure if it's safe man! At my .env we usually store security variables as encryption keys and things like that! Would be better work with my envjs without link it to my Laravel .env!Recce
getenv("APP_ENV") would literally only output what the value of "APP_ENV" is in the .env file - this would be "production", "staging", "development", etc.Mambo
Wow!! It should do the trick! I didn't get it man sorry!! I am gonna try it!! Thanks a lot!Recce
R
8

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"
Redevelop answered 25/4, 2016 at 12:2 Comment(1)
Cool @Jeff!! But how I use that process.env? I mean how to I on my production env that it value is 'production'? Right now my process.env.NODE_ENV is returning undefined!Recce
M
6

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

Mambo answered 25/4, 2016 at 6:49 Comment(3)
I am not sure if it's safe man! At my .env we usually store security variables as encryption keys and things like that! Would be better work with my envjs without link it to my Laravel .env!Recce
getenv("APP_ENV") would literally only output what the value of "APP_ENV" is in the .env file - this would be "production", "staging", "development", etc.Mambo
Wow!! It should do the trick! I didn't get it man sorry!! I am gonna try it!! Thanks a lot!Recce

© 2022 - 2024 — McMap. All rights reserved.