During the research and development of social networking system ThinkSNS+, how to configure Laravel can be configured at the background of the website.
What is ThinkSNS+?
ThinkSNS (TS for short), a platform-wide comprehensive social system, provides social software development and technical solutions for large, small and medium-sized enterprises and entrepreneurs at home and abroad. The latest versions are ThinkSNS+, ThinkSNS V4, ThinkSNS [Jane].
This article shares the use of Laravel Bootstrapping to achieve the website background settings laravel configuration.
Demand scenario
First of all, as a "social system" that users can use, ThinkSNS+ has the same backend as open source website programs, and has some configurations. Laravel is required to be written in the configuration file of / config/*.php, such as the configuration of app.name, app.debug and other information, as well as the driver configuration of Jobs, the configuration of the broadcast system, etc., we have moved to the website backend, and users can mirror the configuration without modifying the configuration file after installation.
How to override configuration
Let's first open the Illuminate\ Foundation\ Application::bootstrapWith method with the following code:
The key code is $this ['events']-> fire (' bootstrapping:'. $bootstrapper, [$this]); and $this ['events']-> fire (' bootstrapped:'. $bootstrapper, [$this]);, which is obviously the pre-and post-events that load and run bootstrapper.
So, let's see, there's another method called beforeBootstrapping and afterBootstrapping, and then what do we do? Let's see.
Yes, the order is fixed here, and my wrong loading order will cause the failure of laravel, so we choose to add an event to the previous application that inherits Illuminate\ Foundation\ Application. The code is as follows:
Where was the event added?
Because ThinkSNS+ inherits Illuminate\ Foundation\ Application to implement the new Application class, we add code directly to the constructor.
In this way, when Laravel starts, but the bootstrapper is not loaded, the post event for loading the configuration has been injected. The post event that I injected will be executed when the load configuration execution is complete.
Implementation of post-event
We have created a class\ Zhiyi\ Plus\ Bootstrap\ LoadConfiguration, registered as a post event, with the path: / app/Bootstrap/LoadConfiguration.php, and the implementation code is as follows:
Quite simply, because app ('config') is an instance of the Illuminate\ Contracts\ Config\ Repository interface, the set method is called directly to configure the override.
The Zhiyi\ Plus\ Support\ Configuration class is an encapsulated custom configuration loading class, and the loaded configuration files are stored in a YAML file, which implements the loading and saving of custom configuration files. In this way, we call API from the background and then constroller calls the save method of this class for persistence.
Zhiyi\ Plus\ Support\ Configuration::getConfigurationBase
Why do you want to talk about this method specifically? Because of the particularity of this method, it is also an important function for depth merge implementation. In Repository, it supports deep key assignment in the form of app.name = value. Using this feature, this function converts a multi-dimensional array into one-dimensional.
Effect:
Then call app ('config')-> set ($arr) to depth merge the config of Laravel.
Finally, the persistent saved YAML content is as follows:
Therefore, based on depth merge in the .plus.yml configuration, you only need to save part of the configuration, and you can merge the Laravel image configuration without the integrity of the configuration structure.
Open the source code repository:
GitHub: https://github.com/zhiyicx/thinksns-plus (click star to follow developments on a daily basis. )