|
4 | 4 | ## Introduction
|
5 | 5 | All your Supercharge configuration files are stored in the `config` directory. Your Supercharge application and the framework core follow these configuration files. You can adjust and tweak the app’s behavior using different values for individual options.
|
6 | 6 |
|
7 |
| -We highly recommend putting all configuration files into the `config` directory because these files are automatically loaded when starting your application. |
8 | 7 |
|
9 |
| -Here’s a preview how Supercharge recommends the usage of environment variables to configure your app: |
| 8 | +## Using Environment Variables |
| 9 | +Applications run in different environments using custom configurations. For example, while in local development you may use a different cache driver then in production. |
10 | 10 |
|
11 |
| - |
| 11 | +Supercharge loads all environment variables configured in a `.env` file when starting your application. A new Supercharge application contains a `.env.example` file. You can copy the `.env.example` file and rename the copied file to `.env` and adjust its values. |
12 | 12 |
|
13 | 13 |
|
14 |
| -## Retrieving Configuration Values |
15 |
| -Supercharge loads all your configuration values into memory when starting the application’s Node.js server. You can access the config values anywhere in your application using the `Config` facade. |
| 14 | +### Environment File Security |
| 15 | +You should not commit your application’s `.env` file to your source control repository. Each developer in your team may require a different configuration. Also, exposing the `.env` file to your source control may leak sensitive credentials in case attackers gain access to the repository. |
16 | 16 |
|
17 |
| -Use the `Config.get` method to retrieve configuration values. All configurations start with the configuration file name followed by a defined key. You may also retrieve nested config values using the “dot” syntax. The dot notation starts with the configuration file name followed by the nested object path: |
18 | 17 |
|
19 |
| -```ts |
20 |
| -import { Config } from '@supercharge/facades' |
| 18 | +### Retrieving Environment Values |
| 19 | +All variables in your `.env` file are loaded into the global [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env) environment object. Instead of accessing environment variables directly on `process.env`, you should use Supercharge’s `Env` facade. |
21 | 20 |
|
22 |
| -const appName = Config.get('app.name', 'My Supercharge App') |
| 21 | +The `Env` facade provides a convenient access the values of environment variables: |
| 22 | + |
| 23 | +```js |
| 24 | +import { Env } from '@supercharge/facades' |
| 25 | + |
| 26 | +const appName = Env.get('APP_NAME') |
23 | 27 | ```
|
24 | 28 |
|
25 |
| -You may also provide a default value as the second argument when retrieving a config value. This default value is used when no config value exists for the provided key: |
| 29 | +You may also provide a default value as the second argument to the `Env` facade when retrieving an environment value: |
26 | 30 |
|
27 | 31 | ```ts
|
28 |
| -const appName = Config.get('app.name', 'My Supercharge App') |
| 32 | +const appName = Env.get('APP_NAME', 'Supercharge') |
29 | 33 | ```
|
30 | 34 |
|
31 | 35 |
|
32 |
| -## Retrieving Environment Values |
33 |
| -All variables in your `.env` file are loaded into the global [`process.env`](https://nodejs.org/docs/latest-v16.x/api/process.html#process_process_env) environment object. |
34 |
| - |
35 |
| -We recommend using environment variables to configure your application. This approach follows the concept of [the 12 factor app](https://12factor.net/). The framework provides a `Env` facade to access the values of environment variables: |
| 36 | +### Ensure Environment Values |
| 37 | +In some situations your application won’t work when a given environment variable is not configured. The `Env` facade allows you to require an environment variable using the `Env.getOrFail` method: |
36 | 38 |
|
37 | 39 | ```js
|
38 | 40 | import { Env } from '@supercharge/facades'
|
39 | 41 |
|
40 |
| -const appName = Env.get('APP_NAME') |
| 42 | +const appKey = Env.getOrFail('APP_KEY') |
41 | 43 | ```
|
42 | 44 |
|
43 |
| -The `Env` facade accepts a default value as the second argument when retrieving the value of an environment variable. This is useful to provide thoughtful default values for your application: |
| 45 | + |
| 46 | +### Determine the Current Environment |
| 47 | +You may want to determine the current application environment. The `Env` facade provides convenience methods to detect the current environment. It compares the `NODE_ENV` environment variable against a given value: |
44 | 48 |
|
45 | 49 | ```ts
|
46 |
| -const appName = Env.get('APP_NAME', 'Supercharge') |
| 50 | +import { Env } from '@supercharge/facades' |
| 51 | + |
| 52 | +const isProduction = Env.isProduction() |
| 53 | +// true when NODE_ENV is set to "production" |
| 54 | + |
| 55 | +const isNotProduction = Env.isNotProduction() |
| 56 | +// true when NODE_ENV is not set to "production" |
| 57 | + |
| 58 | +const isTesting = Env.isTesting() |
| 59 | +// true when NODE_ENV is set to "test" or "testing" |
| 60 | + |
| 61 | +const isLocal = Env.is('local') |
47 | 62 | ```
|
48 | 63 |
|
49 |
| -```info |
50 |
| -The idea in Supercharge is to feed your configuration from environment variables. We recommend to use the `Env` facade in your config files and not directly in any application logic. |
| 64 | + |
| 65 | +## Using Configuration Values |
| 66 | +We highly recommend putting all configuration files into the `config` directory because these files are automatically loaded when your application starts. |
| 67 | + |
| 68 | +Here’s a preview how Supercharge recommends the usage of environment variables to configure your app: |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +### Retrieving Configuration Values |
| 74 | +Supercharge loads all your configuration values into memory when starting the application’s HTTP server or Craft CLI. You can access the config values anywhere in your application using the `Config` facade or via the app instance using the `app.config()` method. |
| 75 | + |
| 76 | +Use the `Config.get` method to retrieve configuration values. All configurations start with the configuration file name followed by a defined key. You may also retrieve nested config values using the “dot” syntax. The dot notation starts with the configuration file name followed by the nested object path: |
| 77 | + |
| 78 | +```ts |
| 79 | +import { Config } from '@supercharge/facades' |
| 80 | + |
| 81 | +const appName = Config.get('app.name') |
51 | 82 | ```
|
52 | 83 |
|
| 84 | +You may also provide a default value as the second argument to `Config.get` when retrieving a config value. This default value is used when no config value exists for the provided key: |
| 85 | + |
| 86 | +```ts |
| 87 | +const appName = Config.get('app.name', 'My Supercharge App') |
| 88 | +``` |
53 | 89 |
|
54 |
| -## Environment Variables |
55 |
| -Applications run in different environments with a custom configuration. For example, while in development you may want a different mailing driver than in production or testing. |
56 | 90 |
|
57 |
| -Supercharge loads all environment variables configured in a `.env` file when starting your application. A new Supercharge application contains a `.env.example` file. You may copy the `.env.example` file and rename the copy to `.env` for new applications. |
| 91 | +### Ensure Configuration Values |
| 92 | +You may need to ensure a configuration value while building an application or community package. For example, a file cache driver requires a caching directory that you need to configure. In such situations, you may use the `Config.ensure` method to require a |
58 | 93 |
|
59 |
| -```warning |
60 |
| -You should not commit your application’s `.env` file to your source control repository. Each developer in your team may require a different configuration. Also, exposing the `.env` file to your source control may leak sensitive credentials in case attackers gain access to the repository. |
| 94 | +```js |
| 95 | +import { Config } from '@supercharge/facades' |
| 96 | + |
| 97 | +Config.ensure('cache.location') |
| 98 | +// throws if the configuration for `cache.location` is missing |
61 | 99 | ```
|
| 100 | + |
| 101 | + |
0 commit comments