search
Homephp教程PHP开发Laravel 5.3 study notes configuration

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.