Giter Club home page Giter Club logo

php-env-loader's Introduction

php-env-loader

Load PHP environment variables from various sources. Built-in implementation loads them from a .json file.

NOTE: Laravel 5 has a built-in implementation to load from an .ini-style .env file, and that is recommended instead of using this library.

Installation

Add npmweb/php-env-loader as a requirement to composer.json:

{
    "require": {
        "npmweb/php-env-loader": "1.*"
    }
}

Update your packages with composer update.

Add the service provider to your Laravel service providers list:

return array(
    ...
    'providers' => array(
        ...
        'NpmWeb\PhpEnvLoader\Laravel\EnvLoaderServiceProvider',
    ),
);

Usage

Create a .env.example.json at your project root and fill it with placeholders for any values you want to load:

{
  "DB_HOST": "localhost",
  "DB_SCHEMA": "myappname",
  "DB_USERNAME": "myappname",
  "DB_PASSWORD": "myappname"
}

Then copy it to .env.json, git-ignore it so it won't be committed to version control, and add real values in there:

{
  "DB_HOST": "db.mycompany.com",
  "DB_SCHEMA": "myappname",
  "DB_USERNAME": "myappname",
  "DB_PASSWORD": "swordfish"
}

You can then use PHP's built-in getenv() method to get these values and fill them into config files or anywhere else you need them:

    ...
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => getenv('DB_HOST'),
        'database'  => getenv('DB_SCHEMA'),
        'username'  => getenv('DB_USERNAME'),
        'password'  => getenv('DB_PASSWORD'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
    ...

);

License

This code is open-sourced under the MIT license. For more information, see the LICENSE file.

php-env-loader's People

Contributors

needbee avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.