网页
综合
更新时间 2026-11-13 17:31:01
Configuration | Laravel 13.x - The clean stack for Artisans and agents
自动蜘蛛
laravel.com
正文约 6000 字
All of the configuration files for the Laravel framework 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.
Laravel can display an overview of your application's configuration, drivers, and environment via the about Artisan command.
If you're only interested in a particular section of the application overview output, you may filter for that section using the --only option:
Or, to explore a specific configuration file's values in detail, you may use the config:show Artisan command:
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.
To make this a cinch, Laravel utilizes the DotEnv PHP library. In a fresh Laravel installation, the root directory of your application will contain a .env.example file that defines many common environment variables. During the Laravel installation process, this file will automatically be copied to .env.
Laravel's default .env file contains some common configuration values that may differ based on whether your application is running locally or on a production web server. These values are then read by the configuration files within the config directory using Laravel's env function.
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.
Any variable in your .env file can be overridden by external environment variables such as server-level or system-level environment variables.
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 Laravel's built-in environment encryption. Encrypted environment files may be placed in source control safely.
Before loading your application's environment variables, Laravel determines if an APP_ENV environment variable has been externally provided or if the --env CLI argument has been specified. If so, Laravel will attempt to load an .env.[APP_ENV] file if it exists. If it does not exist, the default .env file will be loaded.
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:
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:
All of the variables listed in the .env file will be loaded into the $_ENV PHP super-global 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 Laravel configuration files, you will notice many of the options are already using this function:
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.
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:
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:
The current application environment detection can be overridden by defining a server-level APP_ENV environment variable.
Unencrypted environment files should never be stored in source control. However, Laravel allows you to encrypt your environment files so that they may safely be added to source control with the rest of your application.
To encrypt an environment file, you may use the env:encrypt command:
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:
The length of the key provided should match the key length required by the encryption cipher being used. By default, Laravel will use the AES-256-CBC cipher which requires a 32 character key. You are free to use any cipher supported by Laravel's encrypter by passing the --cipher option 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:
When encrypting your environment file, you may use the --readable option to retain visible variable names while encrypting their values:
This will produce an encrypted file with the following format:
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, Laravel automatically detects which format was used, so no additional options are needed for the env:decrypt command.
When using
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。