Jas*_*onK 5 php database settings configuration laravel-5
我正在寻找一种从Laravel 5数据库加载设置/配置的有效方法.设置由a key和value列组成,模型类基本上如下所示:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
protected $table = 'settings';
protected $fillable = ['key', 'value'];
protected $primaryKey = 'key';
}
Run Code Online (Sandbox Code Playgroud)
起初我做了一个简单的辅助函数来完成这项工作.问题是,这将导致每页请求多次调用.哪个慢了.
/**
* 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'
Run Code Online (Sandbox Code Playgroud)
为了改进这一点,我创建了一个Setting在App\Classes目录中调用的自定义类(并为它创建了一个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');
Run Code Online (Sandbox Code Playgroud)
现在我的问题是:这是解决这个问题的最佳方法吗?我现在正在缓存类构造时的所有设置.然后从缓存中检索设置值.
我开始理解L5中的Repository模式,但我还没有.我认为在这种情况下会有点矫枉过正.我很想知道我的方法是否有任何意义.
kjd*_*n84 15
这是Laravel 5.5的更新答案.
首先,为您的settings表创建一个迁移:
Schema::create('settings', function (Blueprint $table) {
$table->increments('id');
$table->string('key');
$table->text('value')->nullable();
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
然后创建一个Setting模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
protected $fillable = ['key', 'value'];
}
Run Code Online (Sandbox Code Playgroud)
现在,在AppServiceProvider您的boot()方法中添加以下内容:
if (Schema::hasTable('settings')) {
foreach (Setting::all() as $setting) {
Config::set('settings.'.$setting->key, $setting->value);
}
}
Run Code Online (Sandbox Code Playgroud)
这将为config('settings.*')数据库中的每个设置创建*密钥.
例如,插入/创建以下设置:
Setting::create([
'key' => 'example',
'value' => 'Hello World',
]);
Run Code Online (Sandbox Code Playgroud)
现在你可以访问config('settings.example'),这将给你Hello World.
更新设置非常简单:
Setting::where('key', 'example')->update([
'value' => 'My New Value',
]);
Run Code Online (Sandbox Code Playgroud)
我的 Laravel 版本是6.0(截至目前最新),我不得不搜索这个问题并找到了解决方案,编译上面的所有答案让我们开始吧。
第一步:在App目录下创建Helpers.php文件
namespace App;
use Cache;
class Helpers
{
/**
* Fetch Cached settings from database
*
* @return string
*/
public static function settings($key)
{
return Cache::get('settings')->where('key', $key)->first()->value;
}
}
Run Code Online (Sandbox Code Playgroud)
第 2 步:使用以下内容创建设置模型
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Settings extends Model
{
protected $fillable = ['key', 'value'];
}
Run Code Online (Sandbox Code Playgroud)
第 3 步:为设置表创建迁移并迁移
Schema::create('settings', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('key')->unique();;
$table->text('value')->nullable();
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
第 4 步:添加到 App\Providers\ServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Cache;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Cache::forever('settings', \App\Models\Settings::all());
}
}
Run Code Online (Sandbox Code Playgroud)
第 5 步:使用 tinker 或任何其他方法添加一些设置
第 6 步:使用
use App\Helpers;到您的控制器dd(Helpers::settings('your_setting_name_here'));好处:
恕我直言,它有点过度设计.您可以使用帮助程序方法执行相同操作:
function settings($key)
{
static $settings;
if(is_null($settings))
{
$settings = Cache::remember('settings', 24*60, function() {
return array_pluck(App\Setting::all()->toArray(), 'value', 'key');
});
}
return (is_array($key)) ? array_only($settings, $key) : $settings[$key];
}
Run Code Online (Sandbox Code Playgroud)
不那么累赘.没有循环.每个请求最多1个DB.每个请求最多1个缓存命中.