Configuration information
- Environment variable type
- Retrieve environment configuration
- Determine the current environment
- Hide environment variables on the debugging page
- Access configuration values
- Configuration Cache
- Maintenance Mode
- ##Maintenance Mode & Queue
- Alternatives to Maintenance Mode
- Introduction
- Environment configuration
- Environment variable type
- Retrieve environment configuration
- Determine the current environment
- ##Hide environment variables on the debugging page
- Access configuration values
- Configuration cache
- Maintenance mode
Configuration
config directory. Each option is described, and you can review these files at any time to familiarize yourself with what configuration options are available to you.
.env.example file. If Laravel is installed through Composer, this file will be automatically renamed
.env. Otherwise, you need to manually change the file name.
.env file should not be committed to your application's source control system, as each developer/server using your application may need to have a different environment configuration. Additionally, in the event that an intruder gains access to your source control repository, this becomes a security risk because any sensitive credentials are exposed.
.env.example file in your application. Because by placing placeholder values in the sample configuration file, other developers on the team can clearly see which environment variables are required to run the application. You can also create a
.env.testing file that will overwrite
.env when running PHPUnit tests or executing Artisan commands with the --env=testing
option. Value in the file.
{tip}Environment variable type.env
All variables in the file can be overridden by external environment variables (such as server-level or system-level environment variables).
.env All variables in the file are parsed as strings, Therefore some reserved values were created to allow you to return more types of variables from the
env() function:
.env 值 | env() 值 |
---|---|
true | (bool) true |
(true) | (bool) true |
false | (bool) false |
(false) | (bool) false |
empty | (string) '' |
(empty) | (string) '' |
null | (null) null |
(null) | (null) null |
If you need to define an environment variable with a value that contains spaces, you can do so by enclosing the value in double quotes.
APP_NAME="我的 应用"
Retrieve environment configuration
When the application receives a request, all the items listed in the .env
file The variable will be loaded into PHP's super global variable $ _ENV
. You can retrieve the values of these variables using the env
function. In fact, if you look at Laravel's configuration file, you'll notice that several options already use this function:
'debug' => env('APP_DEBUG', false),
The second value passed to the env
function is "default" value". If no environment variable exists for the given key, this value will be used.
Determine the current environment
The current environment of the application is passed APP_ENV in the
.env file
Variable determined. You can access this value through the environment
method in the App
facade:
$environment = App::environment();
You can also pass parameters to the environment
method to check Whether the current environment configuration matches the given value. This method will return true
if it matches the given value:
if (App::environment('local')) { // 当前环境是 local } if (App::environment(['local', 'staging'])) { // 当前的环境是 local 或 staging... }
{tip} The current environment of the application can be detected by the server-level
APP_ENV
Environment variable coverage. This is useful when configuring different environments for the same application, so that you can set a given environment to match a given host in your server configuration.
Hide environment variables in the debug page
When an exception is not caught and APP_DEBUG
the environment variable is true
, the debug page will display all environment variables and content. In some cases you may want to hide certain variables. You can do this by setting the debug_blacklist
option in the config/app.php
configuration file.
Some variables are available in environment variables, server or request data. Therefore, you may need to add the $_ENV
and $_SERVER
variables to the blacklist:
return [ // ... 'debug_blacklist' => [ '_ENV' => [ 'APP_KEY', 'DB_PASSWORD', ], '_SERVER' => [ 'APP_KEY', 'DB_PASSWORD', ], '_POST' => [ 'password', ], ], ];
Accessing configuration values
You can easily access configuration values from anywhere in your application using the global config
function. Configuration values can be accessed using "dot" syntax, which contains the name of the file and option to be accessed. You can also specify a default value, which is returned if the configuration option does not exist:
$value = config('app.timezone');
To set the configuration value at runtime, pass an array to the config
function
config(['app.timezone' => 'America/Chicago']);
Configuration Cache
To improve the speed of your application, you should cache all configuration files using the Artisan command config:cache
into a single file. This will combine all the configuration options in your application into a single file, which will then be quickly loaded by the framework.
Generally speaking, you should run the php artisan config:cache
command as part of your regular production environment deployment. This command should not be run in a local development environment because configuration options often need to be changed during application development.
{note} If you execute the
config:cache
command during deployment, you should ensure that you only call theenv
function from within the configuration file. Once the configuration is cached, the.env
file will no longer be loaded and all calls to theenv
function will returnnull
.
Maintenance Mode
When the application is in maintenance mode, all requests to the application are displayed as a custom view . This makes it easy to "shut down" your application when updating or performing maintenance. Maintenance mode checks are included in the application's default middleware stack. If the application is in maintenance mode, a MaintenanceModeException
exception with status code 503 will be thrown.
To enable maintenance mode, simply execute Artisan's down
command below:
php artisan down
You can also provide to the
down command message
and retry
options. The value of the message
option can be used to display or record a custom message, and the retry
value can be used to set the value of Retry-After
in the HTTP request header:
php artisan down --message="Upgrading Database" --retry=60
Even in maintenance mode, you can allow specific IP addresses or networks to access the application using the command allow
option:
php artisan down --allow=127.0.0.1 --allow=192.168.0.0/16
To turn off maintenance mode, use up
Command:
php artisan up
{tip} You can customize the default maintenance mode template by modifying the
resources/views/errors/503.blade.php
template file.
Maintenance Mode & Queue
When the application is in maintenance mode, queue tasks are not processed. These tasks will continue to be processed after the application exits maintenance mode.
Alternatives to Maintenance Mode
Maintenance mode can cause your application to have seconds of downtime (non-responsiveness), so you may consider using an alternative like Envoyer to get zero done with Laravel Downtime deployment.