Tìm kiếm bài viết

Laravel with Vue.js and environmental variables

10.07.2019

0/5 (0 Reviews)

Laravel with Vue.js and environmental variables. So basically, just add whatever environmental variable to your .env files with the prefix of MIX_

    It’s just a tip for someone who;

    1. Uses Laravel, Mix and Vue.js,
    2. Looking for a .env implemantation for a day, a whole day,
    3. Forks one and tries to improve it with dynamic .env file names,
    4. Finds out require() function with webpack can’t use variable module names, because of build-time confusions of it’s nature etc.
    5. Is already aware of Laravel’s .env functionality and asks himself what about that one?
    6. Then figures out there is even a section in their documentation about that!

    So basically, just add whatever environmental variable to your .env files with the prefix of MIX_

    You don’t even have to retype your existing variable values, just use them as they are:

    APP_URL=http://127.0.0.1:8000
    MIX_APP_URL="${APP_URL}"​

    Or you may mix them with new variables:

    APP_URL=http://127.0.0.1:8000
    MIX_GRAPHQL_URI="${APP_URL}/graphql"

    Finally, use them anywhere in your client app:

    const httpLink = new HttpLink({
        uri: process.env.MIX_GRAPHQL_URI
    });
    Vue.axios.defaults.baseURL = process.env.MIX_APP_URL;

    And you can always test your environment with NODE_ENV

    if ( process.env.NODE_ENV == 'production' ) {
         ...do things in production mode
    } else {
         ...or the other way
    }

    Note that you don’t define NODE_ENV in your .env file, it’s already set and value depends on very webpack build.

    CÓ THỂ BẠN QUAN TÂM

    Bài Viết Cùng Chuyên Mục

    XEM THÊM
    thumbnail

    What is UI & UX Design?

    25.04.2019

    What is UI & UX Design? UX Design refers to the term User Experience Design, while UI Design stands for User Interface Design.

    thumbnail

    What is Microservices? Understanding PHP Microservices

    18.04.2019

    Microservice architecture, or simply microservices is a distinctive method of developing software systems that tries to focus on building single-function modules with well-defined interfaces and operations.

    thumbnail

    What is a Webhook? Handling Incoming Webhooks in PHP

    17.04.2019

    Web hooks are a incredibly useful and a resource-light way to implement event reactions. Web hooks provide a mechanism where by a server-side application can notify a client-side application when a new event (that the client-side application might be interested in) has occurred on the server.

    thumbnail

    GitHub: Announcing unlimited free private repositories

    08.01.2019

    GitHub: Announcing unlimited free private repositories for welcome New year 2019.

    thumbnail

    Website Design Service

    07.01.2019

    Web Design Service of LP Company will provide you with a full-featured website Professional, optimal and confidential website quality

    Mục lục bài viết