Laravel background can modify the configuration to automatically load

Source: Internet
Author: User
1, Laravel realization of production environment and development environment configuration separation

Usually we will put the configuration file required parameters in the directory below/config, all the files under this folder will be loaded automatically, we use config (filename + ".") + configuration items), but it is cumbersome to directly modify the configuration of the file under Config, for example, our production environment is different from the development environment, this is more troublesome, can laravel very convenient to us to solve this method, Our development environment configures the. env file in the root directory, and the configuration inside config is read. env to achieve a free switchover of the configuration, OK, we have done a more convenient thing

2, how to achieve some more fixed parameters of the background configuration

Our daily development system some things that are not often changed, such as Site settings, mailbox configuration, third-party login configuration. These configurations we hope that after the background modification of our program can read like a configuration file, eliminating the need for each set of systems we have to manually modify the parameter configuration (assuming that our system is a set of sold to others).

This time the middleware is used, we can build a middleware, such as app/http/middleware/resetconfig.php, and then put this middleware into app/http/kernel.php protected $ In the middleware array, we can implement the write configuration file read into Config

resetconfig.php

Public function handle ($request, Closure $next) {    $json _datas = $arr _datas = null;    Email    $file _path = Storage_path (' App/email.data ');    if (file_exists ($file _path)) {        $json _datas = file_get_contents ($file _path);        $json _datas && $arr _datas = Json_decode ($json _datas, true);        $arr _datas && config ([' mail ' = array_merge (config (' mail '), $arr _datas)]);    }    $json _datas = $arr _datas = null;    msg    $file _path = Storage_path (' App/msg.data ');    if (file_exists ($file _path)) {        $json _datas = file_get_contents ($file _path);        $json _datas && $arr _datas = Json_decode ($json _datas, true);        $arr _datas && config ([' msg ' = $arr _datas]);    }    Return $next ($request);}

kernel.php

protected $middleware = [    \illuminate\foundation\http\middleware\checkformaintenancemode::class,    \app\ Http\middleware\resetconfig::class,    \app\http\middleware\cors::class,//Resolution cross-domain];

This allows us to implement the program only by writing files to App/email.data, and our other programs can access our modified configuration in a timely manner through the Config method.

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