Home  >  Article  >  php教程  >  Laravel 5.3 study notes configuration

Laravel 5.3 study notes configuration

高洛峰
高洛峰Original
2016-12-22 15:58:171146browse

1. Introduction

All configuration files of Laravel are stored in the config directory, and each configuration item has comments to ensure that you can intuitively understand the function and usage of the configuration item when browsing the configuration item of any configuration file.

2. Access configuration values

You can use the global auxiliary function config to access the configuration value anywhere in the application. The configuration value can be accessed in the form of file name + "." + configuration item. When the configuration item is not configured Return to the default value when:

$value = config('app.timezone');

If you want to set the configuration value at runtime, just pass the array parameter to the config method:

config(['app.timezone ' => 'America/Chicago']);

3. Environment configuration

Setting different configuration values ​​based on the environment in which the application is running can bring great convenience to our development. For example, we usually use local This mechanism is easy to implement in Laravel by configuring a different cache driver than the online environment.

Laravel uses the PHP library DotEnv developed by Vance Lucas to implement this mechanism. In the newly installed Laravel, there is an .env.example file in the root directory. If Laravel is installed through Composer, then the file has been copied Name it .env, otherwise you will have to rename the file manually.

Get environment variable configuration values

Every time the application accepts a request, all configurations and their values ​​listed in .env will be loaded into the PHP superglobal variable $_ENV, and then you can use the auxiliary function in the application Function env to obtain these configuration values. In fact, if you look at Laravel's configuration file, you will find that this auxiliary function is already used in many places:

'debug' => env('APP_DEBUG', false),

passed to the env function The second parameter is the default value, which will be the default value if the environment variable is not configured.

Don’t submit .env files to source control (svn or git, etc.) because each developer/server using your application may require different environment configurations.

If you are developing in a team, you need to submit the .env.example file to source control along with your application: place some configuration values ​​​​in the form of placeholders in the .env.example file, This way other developers will know exactly what environment variables need to be configured to run your application.

Determine the current application environment

The current application environment is determined by the APP_ENV variable in the .env file. You can access its value through the environment method of the App facade:

$environment = App::environment();

You You can also pass parameters to the environment method to determine whether the current environment matches the given value. You can even pass multiple values ​​if necessary. This method returns true if the current environment matches the given value:

if (App::environment('local')) {
  // The environment is local
}
 
if (App::environment('local', 'staging')) {
  // The environment is either local OR staging...
}

The application instance can also be accessed through the helper function app:

$environment = app()->environment();

4. Configuration cache

In order to accelerate the application, you can use the Artisan command config:cache to cache the configuration of all configuration files into a single file. This will merge all configuration options into a single file so that it can be quickly loaded by the framework.

Once the application is online, you need to run php artisan config:cache once. However, when developing locally, there is no need to run this command frequently because the configuration values ​​often need to be changed.

5. Maintenance Mode

When your app is in maintenance mode, all requests to the app will return the same custom view. This mechanism makes it easy to "shut down" the site when upgrading or maintaining the application. The maintenance mode judgment code is located in the application's default middleware stack. If the application is in maintenance mode, MaintenanceModeException with status code 503 will be thrown.

To turn on the maintenance mode, just execute the Artisan command down:

php artisan down

To turn off the maintenance mode, the corresponding Artisan command is up:

php artisan up

Maintenance mode response template

The default maintenance mode response view template is resources/views/errors/503.blade.php

Maintenance Mode & Queue

When your site is in maintenance mode, all queue tasks will not be executed; when the application exits Only in maintenance mode will these tasks continue to be processed normally.

Alternatives to Maintenance Mode

Since the execution of maintenance mode commands takes several seconds, you may consider using Envoyer to implement 0-second offline as an alternative.


For more Laravel 5.3 study notes and configuration related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn