Docs / Getting Started / Configuration
Configuration
Introduction
All of the configuration files for Laraflare are stored in the config directory. Each option is documented, so feel free to look through the files and get familiar with the options available to you.
These configuration files allow you to configure things like your database connection information, your mail server information, as well as various other core configuration values such as your application URL and encryption key.
The about Command
Laraflare can display an overview of your application's configuration, drivers, and environment via the about Artisan command.
npx artisan about
If you're only interested in a particular section of the application overview output, you may filter for that section using the --only option:
npx artisan about --only=environment
Or, to explore a specific configuration file's values in detail, you may use the config:show Artisan command:
npx artisan config:show database
Environment Configuration
It is often helpful to have different configuration values based on the environment where the application is running. For example, you may wish to use a different cache driver locally than you do on your production server.
A fresh Laraflare application contains a .env.example file in its root directory that defines many common environment variables. When the application is created, this file is copied to .env.
The default .env file contains some common configuration values that may differ based on whether your application is running locally or on a production Worker. These values are then read by the configuration files within the config directory using the env function (or envString and envBoolean, which return a string or a boolean).
If you are developing with a team, you may wish to continue including and updating the .env.example file with your application. By putting placeholder values in the example configuration file, other developers on your team can clearly see which environment variables are needed to run your application.
Note
Any variable in your
.envfile can be overridden by external environment variables such as system-level environment variables.
Environment File Security
Your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration. Furthermore, this would be a security risk in the event an intruder gains access to your source control repository, since any sensitive credentials would get exposed.
However, it is possible to encrypt your environment file using Laraflare's built-in environment encryption. Encrypted environment files may be placed in source control safely.
Additional Environment Files
Before loading your application's environment variables, Laraflare determines if an APP_ENV environment variable has been externally provided or if the --env CLI argument has been specified. If so, Laraflare will attempt to load an .env.[APP_ENV] file if it exists. If it does not exist, the default .env file will be loaded.
A Laraflare application keeps one file per environment: .env for your machine, and .env.staging and .env.production for the deployed Workers. See Environment Files.
Environment Variable Types
All variables in your .env files are typically parsed as strings, so some reserved values have been created to allow you to return a wider range of types from the env() function:
.env Value |
env() Value |
|---|---|
| true | true |
| (true) | true |
| false | false |
| (false) | false |
| empty | '' |
| (empty) | '' |
| null | null |
| (null) | null |
If you need to define an environment variable with a value that contains spaces, you may do so by enclosing the value in double quotes:
APP_NAME="My Application"
Retrieving Environment Configuration
All of the variables listed in the .env file will be loaded into the environment when your application receives a request. However, you may use the env function to retrieve values from these variables in your configuration files. In fact, if you review the configuration files, you will notice many of the options are already using this function:
debug: envBoolean('APP_DEBUG', false),
The second value passed to the env function is the "default value". This value will be returned if no environment variable exists for the given key.
Determining the Current Environment
The current application environment is determined via the APP_ENV variable from your .env file. You may access this value via the environment method on the App facade:
import { App } from '@laraflare/framework';
const environment = App.environment();
You may also pass arguments to the environment method to determine if the environment matches a given value. The method will return true if the environment matches any of the given values:
if (App.environment('local')) {
// The environment is local
}
if (App.environment(['local', 'staging'])) {
// The environment is either local OR staging...
}
Note
The current application environment detection can be overridden by defining a system-level
APP_ENVenvironment variable.
Encrypting Environment Files
Unencrypted environment files should never be stored in source control. However, Laraflare allows you to encrypt your environment files so that they may safely be added to source control with the rest of your application.
Encryption
To encrypt an environment file, you may use the env:encrypt command:
npx artisan env:encrypt
Running the env:encrypt command will encrypt your .env file and place the encrypted contents in an .env.encrypted file. The decryption key is presented in the output of the command and should be stored in a secure password manager. If you would like to provide your own encryption key you may use the --key option when invoking the command:
npx artisan env:encrypt --key=3UVsEgGVK36XN82KKeyLFMhvosbZN1aF
Note
The length of the key provided should match the key length required by the encryption cipher being used. By default, Laraflare will use the
AES-256-CBCcipher which requires a 32 character key. You are free to use any cipher supported by Laraflare's encrypter by passing the--cipheroption when invoking the command.
If your application has multiple environment files, such as .env and .env.staging, you may specify the environment file that should be encrypted by providing the environment name via the --env option:
npx artisan env:encrypt --env=staging
Readable Variable Names
When encrypting your environment file, you may use the --readable option to retain visible variable names while encrypting their values:
npx artisan env:encrypt --readable
This will produce an encrypted file with the following format:
APP_NAME=eyJpdiI6...
APP_ENV=eyJpdiI6...
APP_KEY=eyJpdiI6...
APP_DEBUG=eyJpdiI6...
APP_URL=eyJpdiI6...
Using the readable format allows you to see which environment variables exist without exposing sensitive data. It also makes reviewing pull requests much easier since you can see which variables were added, removed, or renamed without needing to decrypt the file.
When decrypting environment files, Laraflare automatically detects which format was used, so no additional options are needed for the env:decrypt command.
Note
When using the
--readableoption, comments and blank lines from the original environment file are not included in the encrypted output.
Updating Readable Environment Files
When updating a readable encrypted environment file, Laraflare preserves unchanged values and removes variables that no longer exist in the source environment file:
npx artisan env:encrypt --readable --key=3UVsEgGVK36XN82KKeyLFMhvosbZN1aF
The encrypted file will be created if it does not exist. When updating an existing file, provide the same encryption key and cipher used to create it. If the file cannot be decrypted, the command will fail without overwriting it.
To encrypt all values again, use the --force option. This allows you to change the encryption key or replace an invalid encrypted file:
npx artisan env:encrypt --readable --force --key=3UVsEgGVK36XN82KKeyLFMhvosbZN1aF
Decryption
To decrypt an environment file, you may use the env:decrypt command. This command requires a decryption key, which Laraflare will retrieve from the LARAVEL_ENV_ENCRYPTION_KEY environment variable:
npx artisan env:decrypt
Or, the key may be provided directly to the command via the --key option:
npx artisan env:decrypt --key=3UVsEgGVK36XN82KKeyLFMhvosbZN1aF
When the env:decrypt command is invoked, Laraflare will decrypt the contents of the .env.encrypted file and place the decrypted contents in the .env file.
The --cipher option may be provided to the env:decrypt command in order to use a custom encryption cipher:
npx artisan env:decrypt --key=qUWuNRdfuImXcKxZ --cipher=AES-128-CBC
If your application has multiple environment files, such as .env and .env.staging, you may specify the environment file that should be decrypted by providing the environment name via the --env option:
npx artisan env:decrypt --env=staging
In order to overwrite an existing environment file, you may provide the --force option to the env:decrypt command:
npx artisan env:decrypt --force
Accessing Configuration Values
You may easily access your configuration values using the Config facade or global config function from anywhere in your application. The configuration values may be accessed using "dot" syntax, which includes the name of the file and option you wish to access. A default value may also be specified and will be returned if the configuration option does not exist:
import { Config, config } from '@laraflare/framework';
let value = Config.get('app.timezone');
value = config('app.timezone');
// Retrieve a default value if the configuration value does not exist...
value = config('app.timezone', 'Asia/Seoul');
To set configuration values at runtime, you may invoke the Config facade's set method or pass an object to the config function:
Config.set('app.timezone', 'America/Chicago');
config({ 'app.timezone': 'America/Chicago' });
To assist with static analysis, the Config facade also provides typed configuration retrieval methods. If the retrieved configuration value does not match the expected type, an exception will be thrown:
Config.string('config-key');
Config.integer('config-key');
Config.float('config-key');
Config.boolean('config-key');
Config.array('config-key');
Config.collection('config-key');
Configuration Caching
To give your application a speed boost, you should cache all of your configuration files into a single file using the config:cache Artisan command. This will combine all of the configuration options for your application into a single file which can be quickly loaded by the framework.
You should typically run the npx artisan config:cache command as part of your production deployment process. The command should not be run during local development as configuration options will frequently need to be changed during the course of your application's development.
Once the configuration has been cached, your application's .env file will not be loaded by the framework during requests or Artisan commands; therefore, the env function will only return external, system level environment variables.
For this reason, you should ensure you are only calling the env function from within your application's configuration (config) files. You can see many examples of this by examining Laraflare's default configuration files. Configuration values may be accessed from anywhere in your application using the config function described above.
The config:clear command may be used to purge the cached configuration:
npx artisan config:clear
Warning
If you execute the
config:cachecommand during your deployment process, you should be sure that you are only calling theenvfunction from within your configuration files. Once the configuration has been cached, the.envfile will not be loaded; therefore, theenvfunction will only return external, system level environment variables.
Configuration Publishing
Most of Laraflare's configuration files are already published in your application's config directory; however, certain configuration files like cors.ts and view.ts are not published by default, as most applications will never need to modify them.
However, you may use the config:publish Artisan command to publish any configuration files that are not published by default:
npx artisan config:publish
npx artisan config:publish --all
Debug Mode
The debug option in your config/app.ts configuration file determines how much information about an error is actually displayed to the user. By default, this option is set to respect the value of the APP_DEBUG environment variable, which is stored in your .env file.
Warning
For local development, you should set the
APP_DEBUGenvironment variable totrue. In your production environment, this value should always befalse. If the variable is set totruein production, you risk exposing sensitive configuration values to your application's end users.
Maintenance Mode
When your application is in maintenance mode, a custom page will be displayed for all requests into your application. This makes it easy to "disable" your application while it is updating or when you are performing maintenance. A maintenance mode check is included in the default middleware stack for your application. If the application is in maintenance mode, an HttpException instance will be thrown with a status code of 503.
To enable maintenance mode, execute the down Artisan command:
npx artisan down
If you would like the Refresh HTTP header to be sent with all maintenance mode responses, you may provide the refresh option when invoking the down command. The Refresh header will instruct the browser to automatically refresh the page after the specified number of seconds:
npx artisan down --refresh=15
You may also provide a retry option to the down command, which will be set as the Retry-After HTTP header's value, although browsers generally ignore this header:
npx artisan down --retry=60
Bypassing Maintenance Mode
To allow maintenance mode to be bypassed using a secret token, you may use the secret option to specify a maintenance mode bypass token:
npx artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"
After placing the application in maintenance mode, you may navigate to the application URL matching this token and Laraflare will issue a maintenance mode bypass cookie to your browser:
https://example.com/1630542a-246b-4b66-afa1-dd72a4c43515
If you would like Laraflare to generate the secret token for you, you may use the with-secret option. The secret will be displayed to you once the application is in maintenance mode:
npx artisan down --with-secret
When accessing this hidden route, you will then be redirected to the / route of the application. Once the cookie has been issued to your browser, you will be able to browse the application normally as if it was not in maintenance mode.
Note
Your maintenance mode secret should typically consist of alpha-numeric characters and, optionally, dashes. You should avoid using characters that have special meaning in URLs such as
?or&.
Maintenance Mode on Multiple Servers
By default, Laraflare determines if your application is in maintenance mode using a file-based system. This means to activate maintenance mode, the npx artisan down command has to be executed on each machine hosting your application.
Note
A deployed Worker has no local file to read, so a live application uses the cache-based method below.
Alternatively, Laraflare offers a cache-based method for handling maintenance mode. This method requires running the npx artisan down command just once. To use this approach, modify the maintenance mode variables in your application's .env file. You should select a cache store that is accessible everywhere your application runs. This ensures the maintenance mode status is consistently maintained across every instance:
APP_MAINTENANCE_DRIVER=cache
APP_MAINTENANCE_STORE=database
Pre-Rendering the Maintenance Mode View
Laraflare allows you to pre-render a maintenance mode page that will be returned at the very beginning of the request cycle. You may pre-render an HTML file of your choice using the down command's render option:
npx artisan down --render="resources/views/errors/503.html"
Note
The screens are Vue, so there is no Blade template to render; the
renderoption names an HTML file, which is read whendownruns and sent as the response while the application is down.
Redirecting Maintenance Mode Requests
While in maintenance mode, Laraflare will display the maintenance mode page for all application URLs the user attempts to access. If you wish, you may instruct Laraflare to redirect all requests to a specific URL. This may be accomplished using the redirect option. For example, you may wish to redirect all requests to the / URI:
npx artisan down --redirect=/
Disabling Maintenance Mode
To disable maintenance mode, use the up command:
npx artisan up
Maintenance Mode and Queues
While your application is in maintenance mode, no queued jobs will be handled. The jobs will continue to be handled as normal once the application is out of maintenance mode.