kromacie / l5settings
允许您将设置存储在数据库中并缓存的包。
Requires
- ext-json: *
This package is not auto-updated.
Last update: 2024-09-23 07:48:57 UTC
README
您想要比基于文件的Laravel提供的更动态的配置系统吗?这是使用此包的好理由。您可以使用简单的外观从数据库中编辑/创建和检索设置。此外,它支持JSON解码和编码,因此您还可以存储可JSON化的值。
内容
要求
此包是用PHP 7.2为Laravel 5.7和Lumen 5.7编写的,因此我不保证在旧版本上它将稳定工作。
设置
要下载此包,只需在您的项目终端中写入
$ composer require kromacie/l5settings
当包被下载后,注册服务提供者。
对于Lumen
$app->register(\Kromacie\L5Settings\SettingsServiceProvider::class);
对于Laravel,这应该会自动发现,但如果不会,请使用与上面相同的命名空间在config/app.php中注册它。
接下来,复制或提供配置文件。您可以在"vendor/kromacie/l5settings/config"目录中找到它。
现在,您必须提供迁移文件并运行该迁移。否则,注册设置将抛出SettingsNotLoaded异常。
最后,您应该在配置文件中将enable设置为true。
配置
配置文件包含许多参数,可以帮助您适应设置应该如何存储、检索,以及哪些参数应该被覆盖。
<?php
return [
/*
* Enable or disable this package.
*/
'enabled' => false,
/*
* Here you can set which Laravel configuration values should be overwritten by settings
* from database.
*/
'overwrite' => [
],
'database' => [
/**
* Set the connection of using database.
*/
'connection' => 'mysql',
/*
* Just set how your table for storing settings should be named.
*/
'table' => 'settings',
],
'cache' => [
/*
* Set which cache store should be used to store your settings.
*/
'store' => 'redis',
/*
* All of the settings retrived from database will be stored under this cache key.
* You can change this as you wish.
*/
'key' => 'settings',
/*
* Time in minutes of - "How long values should be cached?"
*/
'expiration_time' => 60,
],
/*
* Set the prefix under which you can find your settings using config function.
*/
'prefix' => 'global',
/*
* You can set custom SettingManager handler to resolve how your settings should be
* stored or retrived.
*/
'manager' => \Kromacie\L5Settings\SettingsManager::class,
/*
* You can set your custom model if u want to change some logic inside
*/
'model' => \Kromacie\L5Settings\Models\Setting::class
];
用法
数据库
此包的用法非常简单。配置后,如果您想与数据库交互,应该使用Settings外观。它包含一些重要的方法,如
- @method static mixed set($key, $value);
- @method static mixed get($key);
- @method static boolean has($key);
- @method static array all();
- @method static void remove($key);
- @method static void removeAll();
set方法负责在数据库中创建和更新字段。get、has、all方法在调用时执行相同的操作。此外,使用set、remove和removeAll将刷新您的设置缓存并重新注册它们。
我不建议在生产环境中使用remove和removeAll,因为设置不应该被删除。
### 缓存如果您不想使用数据库连接,您应该使用Config/Repository中的配置来检索您的配置。
例如,如果您有一个名为"global"的前缀,所有从数据库检索的设置都将位于此前缀下。如果您想获取检索到的设置之一,您应该使用如下方式
$foo = config('global.foo');
或使用外观
$foo = Config::get('global.foo');
在注入Config/Repository之后。
public function controllerEndpoint(Repository $config)
{
$foo = $config->get('global.foo');
}
### 覆盖
在某些情况下,使用设置覆盖其他配置可能很有用。假设您想要更改应用程序的默认语言。您可以在数据库中将其存储在'fallback_locale'下,并将此参数添加到配置文件中。
/**
* config/l5settings.php
*/
return [
'overwrite' => [
'fallback_locale' => 'app.fallback_locale'
]
]
现在,在服务提供者启动后,app.fallback_locale将包含与数据库中fallback_locale相同的值。
我希望您会喜欢这个包。如果您发现任何问题,请向我报告。谢谢。