How to do Laravel configuration can be site background configuration "social system thinksns+ development diary Four"

Source: Internet
Author: User

It was almost a week from the last share, This article shares the use of Laravel Bootstrapping reach the site background settings laravel configuration.

Demand Scenarios

First of all, thinksns+ as a user can use the "social system" and open-source web site programs like the background, there are some configuration, Laravel is required to write in/config/*.php configuration file, such as App.name, App.debug and other information configuration, As well as the driver configuration of Jobs, the configuration of the broadcast system, etc., we have moved to the site background, users can be installed after installation without modifying the configuration files in the case of mirroring configuration.

How to override the configuration

We first open the Illuminate\foundation\application::bootstrapwith method with the following code:

Key codes in $this [' Events ']->fire (' bootstrapping: '. $bootstrapper, [$this]); and $this [' Events ']->fire (' bootstrapped: '. $bootstrapper, [$this]); , it is obvious that the bootstrapper and post events are loaded and running.

So, let's see another method called Beforebootstrapping and afterbootstrapping. Let's see.

Yes, here is a fixed order, my wrong load order, will cause laravel failure, so we choose to inherit illuminate\foundation\application before the application based on an event, the code is as follows:

Where to add events

Since thinksns+ is inheriting the illuminate\foundation\application implementation of the new application class, we have added code directly to the constructor method.
This way, when Laravel is started, but the bootstrapper has not yet been loaded, the post-event loading configuration has been injected. The post-event I injected is executed when the load configuration execution is complete.

implementation of post-event

We are creating a class such as \zhiyi\plus\bootstrap\loadconfiguration, which is registered as a post event with the path:/app/bootstrap/loadconfiguration.php, and then the implementation code is as follows:

Simply because the app (' config ') is an instance of an Illuminate\contracts\config\repository interface, call the set method directly to configure overrides.
While the Zhiyi\plus\support\configuration class is a packaged custom configuration load class, the loaded configuration file is stored in a YAML file, which implements the loading and saving of the custom configuration file. In this way, we invoke the API from the background and then Constroller call the Save method of this class to persist.

Zhiyi\plus\support\configuration::getconfigurationbase

Why do you have to say this in a special way? Because of the particularity of this method, is also an important function of depth merge implementation, in the Repository support App.name = value in the form of deep once key value assignment, using this feature, this function converts a multidimensional array into one dimension.
Effect:

Then call the app (' config ')->set ($arr) to depth merge the Laravel config.
Finally, the persisted YAML content is as follows:

So, based on the depth merge in the. PLUS.YML configuration, you only need to save a partial configuration to merge the Laravel mirror configuration without configuring the integrity of the structure.

Open source version of the official source of the original access to the only channel: pay attention to the public "Thinksns", reply "open source" to obtain the latest source compressed file address.

Open Source code warehouse:

Github:https://github.com/zhiyicx/thinksns-plus (click Star to focus on development trends daily.) )

Official website: http://www.thinksns.com/

Open source is not easy, in order to strive for open source, our team has made a lot of efforts. The Laravel-based work is presented in front of everyone, and the column will continue to share the technical details of the THINKSNS + development process.

Can see here, must be true love ~ Thank you for your Love

How to do Laravel configuration can be site background configuration "social system thinksns+ development diary Four"

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.