hepplerdotnet / laravel-config-override
存储 Laravel 配置到数据库的包
1.2
2021-12-16 10:41 UTC
Requires
- php: ~8.0
- laravel/framework: ~8.37
Requires (Dev)
- laravel/laravel: ~8.37
This package is auto-updated.
Last update: 2024-09-16 16:55:00 UTC
README
将 Laravel 配置存储在数据库中并通过 config()
辅助函数使用
重要
此包使用 Illuminate\Vendor\HepplerDotNet\Providers\ConfigurationProvider
在你开始大喊“使用 Illuminate 命名空间是不良做法”之前!
你说得对,但让我们看看 Illuminate\Foundation\Application::registerConfiguredProviders
/** * Register all of the configured providers. * * @return void */ public function registerConfiguredProviders() { $providers = Collection::make($this->make('config')->get('app.providers')) ->partition(function ($provider) { return strpos($provider, 'Illuminate\\') === 0; }); $providers->splice(1, 0, [$this->make(PackageManifest::class)->providers()]); (new ProviderRepository($this, new Filesystem, $this->getCachedServicesPath())) ->load($providers->collapse()->toArray()); }
- 它从你的 App 配置提供者数组创建一个集合
- 将此数组分为两块[以 Illuminate 开头的所有内容,其余部分]
- 在中间添加所有 composer 包服务提供者
此包源自我的一个项目,该项目有一些要求
- 对 Active Directory 进行登录(通过 https://ldaprecord.com/docs/laravel/v2/ 解决)
- 通过 Webgui 维护整个 App 配置,包括 ldap 配置
注册服务提供者的所有“常规”选项都失败了,要么是 Auth
门面失败,要么是 LDAP 失败,因为此时 config()
无法访问数据库中的配置。
因此,使用 Illuminate 命名空间是唯一可行的解决方案。就是这样。
安装
由于匿名迁移,需要至少 laravel/framework 8.37
运行 composer require hepplerdotnet/laravel-config-override
注意
数据库中的键 > 值将优于配置或 .env 文件!
组将与现有配置合并
用法
假设你想更改应用程序的区域设置
use HepplerDotNet\LaravelConfigOverride\Models\Group; Group::create(["name" => "app", "root" => true, "active" => true]) ->entries() ->create(["key" => "locale", "value" => "de", "type" => "String"]);
现在你可以通过 config("app.locale")
访问它
或者一个更复杂的示例,具有嵌套组
Group::create(["name" => "mail", "root" => true, "active" => true]) ->groups()->create(["name" => "mailers", "root" => false, "active" => true]) ->groups()->create(["name" => "smtp", "root" => false, "active" => true]) ->entries()->create(["key" => "host","value" => "localhost", "type" => "String"]);
现在你可以通过 config("mail.mailers.smtp.host")
访问它
模型
组
条目
类型密码将在数据库中加密存储!
配置
配置不是真正的 Eloquent 模型,它只是一个从数据库构建配置的类