Laravel calling different configuration files in different environments

Source: Internet
Author: User
Reprint Please specify: Reprint from Yuansir-web Rookie | Lamp Learning Notes

This article link address: Laravel different configuration files in different environments

Laravel how to invoke different configuration files in different environments? Community this question is very many, how graceful method realizes, there should be a lot of methods, I generally used to use two methods, set environment variables, or write the environment value to the. env file, do not know is not elegant, there is a more elegant way to welcome the notice.

1. Set the environment variables, such as now have the local development and the online two environment

Configure PHP-FPM locally (for example, with PHP-FPM instead of Apache), such as environment variables dev_env, modify php-fpm.conf

Env[dev_env]=local

Then set the environment variables for the local system

echo "Export dev_env=local" >>  /etc/profilesource/etc/profile

Let Laravel introduce the appropriate configuration file, add the following code to bootstrap/app.php

$environment = getenv (' dev_env ')? '.' . getenv (' dev_env '): '; $app->loadenvironmentfrom ('. ENV '. $environment);

Build the. Env.local in the Laravel project, where the configuration is the configuration of the local development environment. Env is an online environment, and of course you can create other configuration files such as. Env.develop,.env.preview, etc. Add configuration files and environment variables according to the actual situation, and under different environments, different configurations are invoked according to the environment variables. The advantage of this way is that the project will not need to set up after the launch, the line is not configured on the DEV_ENV environment variable, so the online project is automatically called. ENV configuration

2. Write the name of the current environment in. Env, for example, there are now local development and two environments on the line

Empty the. env file, only the first line is written to the currently running environment, such as local

The new. Local.env and. Production.env are then written to local and online configuration items respectively

Add the following code to the bootstrap/app.php

$env = $app->detectenvironment (function () {    $environmentPath = __dir__. '/.. /.env ';    $SETENV = Trim (file_get_contents ($environmentPath));    if (file_exists ($environmentPath)) {        putenv ("app_env= $setEnv");        if (getenv (' app_env ') && file_exists (__dir__. '/.. /.' . getenv (' app_env '). '. Env ') {            dotenv::load (__dir__. '/.. /', '.' . getenv (' app_env '). '. env ');}}    );

This makes it possible to invoke the corresponding configuration file based on the value written by the. env file.

If there is a more elegant way to welcome advice.

  • 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.