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.