tarfin-labs / laravel-config
Laravel 的键值配置管理
5.1.1
2024-05-02 06:45 UTC
Requires
- php: ^8.1|^8.2|^8.3
- ext-json: *
- illuminate/support: ^8.49|^9.0|^10.0|^11.0
- laravel/legacy-factories: ^1.0
Requires (Dev)
- orchestra/testbench: ^6.13|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.5.2
- dev-master
- 5.1.1
- v5.1.0
- v5.0.2
- v5.0.1
- v5.0.0
- v4.7.0
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.0
- 4.1.0
- 4.0.0
- v3.0.0
- 1.2.0
- 1.1.0
- 1.0.0
- dev-laravel-11-upgrade
- dev-php-8-2-support
- dev-laravel-10-upgrade
- dev-laravel-9-support
- dev-tag-feature
- dev-php8-support
- dev-nested-config-params
- dev-laravel-8-compatibility
- dev-adding-helpers-to-document
- dev-laravel-7-upgrade
- dev-adding-helper
- dev-publishing-factory
This package is auto-updated.
Last update: 2024-09-02 07:29:12 UTC
README
介绍
Laravel config 为您的 Laravel 应用程序提供了一个简单的配置系统。
安装
您可以通过 composer 安装此包。
composer require tarfin-labs/laravel-config
接下来,您应该使用 vendor:publish Artisan 命令发布 Laravel config 迁移文件。
php artisan vendor:publish --provider="TarfinLabs\LaravelConfig\LaravelConfigServiceProvider" --tag="laravel-config"
如果您想使用 Laravel Config 数据库工厂,您也可以使用以下命令发布它:
php artisan vendor:publish --provider="TarfinLabs\LaravelConfig\LaravelConfigServiceProvider" --tag="laravel-config-factories"
最后,您应该运行您的数据库迁移。
php artisan migrate
文档
在您的 Laravel 应用程序中简单使用 laravel-config 包的示例。
创建新的配置参数
$factory = new ConfigFactory(); $configItem = $factory->setName('key') ->setType(ConfigDataType::BOOLEAN) ->setValue('1') ->setTags(['system'])//optional ->setDescription('Lorem ipsum dolor sit amet') ->get(); LaravelConfig::create($configItem);
通过配置名称获取值
LaravelConfig::get('key');
通过配置名称和值设置值
LaravelConfig::set('key', 'value');
获取所有配置参数
LaravelConfig::all();
通过标签获取配置项
LaravelConfig::getByTag('key');
检查配置是否存在
LaravelConfig::has('key');
使用新值更新配置
$factory = new ConfigFactory($configId); $configItem = $factory->setName('updated-key') ->setType(ConfigDataType::BOOLEAN) ->setValue('0') ->setTags(['system'])//optional ->setDescription('updated description') ->get(); LaravelConfig::update($configItem);
删除配置
LaravelConfig::delete('key');
嵌套参数
假设您有一个名为 foo.bar
和 foo.baz
的配置参数。您可以使用 getNested()
方法获取 foo
命名空间下的所有参数。
用法
LaravelConfig::getNested('foo');
输出: Illuminate\Support\Collection
=> Illuminate\Support\Collection {#3048 all: [ TarfinLabs\LaravelConfig\Config\Config {#3097 id: 1, name: "bar", type: "boolean", val: "0", description: null, created_at: "2021-05-06 11:35:05", updated_at: "2021-05-06 11:35:05", }, TarfinLabs\LaravelConfig\Config\Config {#3099 id: 2, name: "baz", type: "boolean", val: "1", description: null, created_at: "2021-05-06 11:03:48", updated_at: "2021-05-06 11:03:48", }, ], }
辅助函数
您还可以使用辅助函数
// Creating config item $factory = new ConfigFactory(); $configItem = $factory->setName('key') ->setType(ConfigDataType::BOOLEAN) ->setValue('1') ->setTags(['system'])//optional ->setDescription('Lorem ipsum dolor sit amet') ->get(); create_config($configItem); // Reading config item read_config('key'); // Checking if the config item exists has_config('key'); // Shortcut to update the value of config item set_config_value('key', 'value'); // Updating config item $factory = new ConfigFactory($configId); $configItem = $factory->setName('updated-key') ->setType(ConfigDataType::BOOLEAN) ->setTags(['system'])//optional ->setValue('0') ->setDescription('updated description') ->get(); update_config($configItem); // Removing config item delete_config('key'); // Reading nested config items read_nested('foo.bar');
自定义转换器
您还可以自定义转换器
$config = factory(Config::class)->create([ 'name' => 'custom-cast-config-name', 'val' => [ConfigDataType::DATE], 'type' => AsEnumCollection::class.':'.ConfigDataType::class, ]);
测试
composer test
更新日志
请参阅 更新日志 以获取最近更改的更多信息。
贡献
欢迎提交拉取请求。对于主要更改,请首先打开一个问题来讨论您想要更改的内容。
请确保适当地更新测试。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件发送到 development@tarfin.com,而不是使用问题跟踪器。
鸣谢
许可
Laravel config 是开源软件,受 MIT 许可证 许可。