Laravel 5.3 The configuration of learning notes _php skills

Source: Internet
Author: User

1. Introduction

All of the configuration files for laravel are stored in the Config directory, and each configuration item is commented to ensure that any configuration item browsing any profile can visually understand the role and usage of the configuration item.

2, Access configuration values

You can access the configuration value at any point in the application using the Global Accessibility function config, which can be a filename + "." + Configuration item to access the default value when the configuration item is not configured:

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

If you want to set the configuration value at run time, pass the array parameter to the Config method:

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

3. Environment configuration

Depending on the application run environment different settings different configuration values can bring great convenience to us, for example, we usually configure the local and online environment different cache drivers, this mechanism in the laravel is very easy to implement.

Laravel uses Vance Lucas to develop PHP library dotenv to implement this mechanism, in the newly installed laravel, the root directory has a. env.example file, if Laravel is installed through Composer, then the file has been is renamed to. Env, otherwise you will have to manually rename the file yourself.

Get environment variable configuration value

All the configurations and their values listed in the. env are loaded into the PHP hyper-global variable $_env each time the application accepts the request, and then you can get the configuration values in the application by using the helper function env. In fact, if you look at the laravel configuration file, you'll find that many places are already using this helper function:

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

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

Do not submit a. env file to source control (svn or GIT, etc.) because each developer/server using your application may require a different configuration of the environment.

If you are developing on a team, you need to submit the. env.example file to the source control with your application: Place some of the configuration values in the. env.example file as placeholders, so that other developers will know exactly what environment variables to run your application to configure.

Determining the current application environment

The current application environment is determined by the APP_ENV variable in the. env file, and you can access its value through the environment method of the APP façade:

$environment = App::environment();

You can also pass parameters to the environment method to determine whether the current environment matches a given value, and you can even pass multiple values if necessary. If the current environment matches the given value, the method returns true:

if (app::environment (' local ')) {
  //the Environment be local
}

if (app::environment (' local ', ' staging ')) { c7/>//the environment is either a local OR staging ...
}

Application examples can also be visited via the helper function app:

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

4. Configure caching

To speed up the application, you can use the Artisan command Config:cache to cache all configuration files in a single file, which merges all the configuration options into a single file and can be quickly loaded by the framework.

Once the application is online, the PHP artisan Config:cache is run once, but it is not necessary to run the command frequently when developing locally, because the configuration values often need to be changed.

5. Maintenance Mode

When your application is in maintenance mode, all requests for the application return to the same custom view. This mechanism makes it easy to "close" a site when upgrading or maintaining an application. The code for the maintenance mode is located in the application default middleware stack, and if the application is in maintenance mode, the maintenancemodeexception with a status code of 503 will be thrown.

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

php artisan down

To turn off 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 & Queues

When your site is in maintenance mode, all queue tasks are not executed, and the tasks will continue to function normally when the application exits maintenance mode.

Alternative to Maintenance mode

Because the Maintenance Mode command takes a few seconds to execute, you may consider using Envoyer to implement a 0-second offline as an alternative.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.