php - Laravel 从数据库加载设置

标签 php database configuration laravel-5 settings

我正在寻找一种使用 Laravel 5 从数据库加载设置/配置的有效方法。设置由 keyvalue 列组成,基本上是模型类看起来像这样:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Setting extends Model
{
    protected $table = 'settings';
    protected $fillable = ['key', 'value'];
    protected $primaryKey = 'key';
}

起初我做了一个简单的辅助函数来完成这项工作。问题是,这会导致每个页面请求多次调用。速度越来越慢。

/**
 * Get the value for the given setting from the database.
 *
 * @param  string  $key
 * @return string
 */
function setting($key)
{
    $setting = Setting::whereKey($key)->firstOrFail();

    return $setting->value;
}

// $foo = setting('foo'); returns 'bar'

为了改进这一点,我在 App\Classes 目录中创建了一个名为 Setting 的自定义类(并为其创建了一个 Facade):

<?php

namespace App\Classes;

use Cache;

class Setting {

    /**
     * The array of settings
     *
     * @var array $settings
     */
    protected $settings = [];

    /**
     * Instantiate the class.
     */
    public function __construct()
    {
        $this->loadSettings();
    }

    /**
     * Pull the settings from the database and cache them.
     *
     * @return void;
     */
    protected function loadSettings()
    {
        $settings = Cache::remember('settings', 24*60, function() {
            return \App\Setting::all()->toArray();
        });

        $this->settings = array_pluck($settings, 'value', 'key');
    }

    /**
     * Get all settings.
     *
     * @return array;
     */
    public function all()
    {
        return $this->settings;
    }

    /**
     * Get a setting value by it's key.
     * An array of keys can be given to retrieve multiple key-value pair's.
     *
     * @param  string|array  $key;
     * @return string|array;
     */
    public function get($key)
    {
        if( is_array($key) ) {
            $keys = [];

            foreach($key as $k) {
                $keys[$k] = $this->settings[$k];
            }

            return $keys;
        }

        return $this->settings[$key];
    }

}

// $foo = Setting::get('foo');

现在我的问题是:这是解决这个问题的最佳方法吗?我现在在构建类时缓存所有设置。然后从缓存中检索设置值。

我开始理解 L5 中的 Repository 模式,但我还没有理解。我认为在这种情况下这太过分了。我很想听听我的方法是否有意义。

最佳答案

这是 Laravel 5.5 的更新答案。

首先,为您的settings 表创建迁移:

Schema::create('settings', function (Blueprint $table) {
    $table->increments('id');
    $table->string('key');
    $table->text('value')->nullable();
    $table->timestamps();
});

然后创建一个Setting模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Setting extends Model
{
    protected $fillable = ['key', 'value'];
}

现在,在 AppServiceProvider 中,将以下内容添加到您的 boot() 方法中:

if (Schema::hasTable('settings')) {
    foreach (Setting::all() as $setting) {
        Config::set('settings.'.$setting->key, $setting->value);
    }
}

这将为数据库中的每个设置创建 config('settings.*'),其中 * 是键。

例如,插入/创建以下设置:

Setting::create([
    'key' => 'example',
    'value' => 'Hello World',
]);

现在您可以访问 config('settings.example'),这将为您提供 Hello World

更新设置就这么简单:

Setting::where('key', 'example')->update([
    'value' => 'My New Value',
]);

关于php - Laravel 从数据库加载设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32824781/

相关文章:

tomcat - 收集 RELOAD 后 SolrCloud 节点关闭

debugging - 无法调试 pic32 微 Controller 或更新配置位

php - 在 php 中将年添加到今天的日期

javascript - 单击jquery清除输入值

database - 将 i18n 数据存储在数据库中是否可以?

iOS mobileconfig 解决方法

php - 在时隙之间查找具有给定任务的空间

php - 一行打印所有内容

java - Android SQLite 从数据库中删除特定行

database - 在 Flutter 中从数据库填充 GridView