ThinkSNS + social system in the development process, how to do Laravel configuration can be configured website background

What is ThinkSNS +

  ThinkSNS (referred to as TS), a fully integrated platform for the social system, the provision of social software development and technical solutions for domestic and foreign medium and small enterprises and entrepreneurs.

 

This Share Laravel under utilization of Bootstrapping reach the site backend configuration settings laravel.

TS + 4 copies .png development diary

Demand scenario

First, ThinkSNS + as a "social system" and open source web application that can be used as a user has the background, there are some configuration, Laravel is asked to write in /config/*.php the configuration file, for example app.name, app.debug configuration and other information, and the circumstances Jobs drive configuration, the configuration of the broadcasting system, and we have moved to the background site, the user can install without having to modify the configuration file configuration image.

How to override configuration

We first opened Illuminate \ Foundation \ Application :: bootstrapWith method, as follows:

Picture 1.png program

Key code $ this [ 'events'] -> fire ( 'bootstrapping:'. $ Bootstrapper, [$ this]); and $ this [ 'events'] -> fire ( 'bootstrapped:' $ bootstrapper, [$. this]); on, apparently to load and run the bootstrapper's pre- and post-event.

So, we see there is a method called beforeBootstrapping afterBootstrapping and then how to do it? We look

Picture 2.png program

Yes, there is a fixed order, I loaded the wrong order can cause failure laravel, so we choose to inherit before applying foundation Illuminate \ Foundation \ Application event on the increase, as follows:

Picture 3.png program

Where to add events

因为 ThinkSNS+ 是继承了 Illuminate\Foundation\Application 实现了新的 Application 类,所以我们直接在构造方法里面增加了代码。
这样,当 Laravel 启动,但是还没有加载 bootstrapper 的时候,已经把 加载配置的后置事件注入进去了。当加载配置执行完成后就会执行我注入的后置事件。

后置事件的实现

我们在创建了 \Zhiyi\Plus\Bootstrap\LoadConfiguration 这样一个类,注册为后置事件,路径为: /app/Bootstrap/LoadConfiguration.php ,然后实现代码如下:

111.jpg

很简单,因为 app('config') 是一个 Illuminate\Contracts\Config\Repository 接口的实例,所以直接调用 set 方法进行配置覆盖。
而 Zhiyi\Plus\Support\Configuration 类是封装的自定义配置加载类,加载的配置文件存放在一个 YAML 文件中,这个类实现了 自定义配置文件的加载和保存。这样,我们从后台调用 API 然后 constroller 调用这个类的 save 方法进行持久化。

Zhiyi\Plus\Support\Configuration::getConfigurationBase

为什么要特殊说一下这个方法?因为这个方法的特殊性,也是 depth merge 实现的重要函数,在 Repository 中支持 app.name = value 这样的形式进行深曾键值赋值,利用这一个特性,这个函数将 多维数组转换为一维。
效果:

Picture 5.png program

Then call app ( 'config') -> set ($ arr) to the config on Laravel were depth merge.
Finally, the persistence saved YAML reads as follows:

Picture 6.png program

Therefore, based on the depth merge .plus.yml configuration, only the configuration storage section, you do not want to the integrity of the arrangement configuration of the combined image Laravel.

Source licenses purchased

Current stabilization system V4 series of all end products (PC / H5 / APP) Authorization to purchase the source code, product experience, please call: 18108035545

Genuine official source open source version gets a unique channels: public concern No. "thinksns", reply "open source version" to get the latest source archive file address.

Open source code repository:

GitHub: https://github.com/zhiyicx/thinksns-plus (Click star, concerned about the development of dynamic daily.)

Guess you like

Origin www.cnblogs.com/thinkSNS/p/11003371.html