buzz / laravel-setting
为laravel 5.*配置包更改
v1.4.7
2017-07-11 13:33 UTC
Requires
- php: >=5.4.0
- illuminate/contracts: 5.0.*|5.1.*|5.2.*|5.3.*|5.4.*
- illuminate/support: 5.0.*|5.1.*|5.2.*|5.3.*|5.4.*
README
安装
打开文件 composer.json
并添加 require
{ "require": { "buzz/laravel-setting": "1.*" } }
然后运行 composer update
命令进行安装。
或者运行以下命令来自动添加到 composer.json
并安装:
composer require buzz/laravel-setting
打开文件 config/app.php
并添加 ServiceProvider。
'providers' => [
//.....
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
//.....
\Buzz\LaravelSettingServiceProvider::class,
],
配置
使用以下命令创建配置文件 config/setting.php
php artisan vendor:publish --provider="Buzz\LaravelSettingServiceProvider"
或者
php artisan vendor:publish
setting.php
<?php
return [
'path' => storage_path('settings.json'),//đường dẫn tới file `setting.json`, nên đặt ở `storage` hoặc `resources`
'auto_alias' => true,//tự động tạo thêm alias `Setting`
'auto_save' => true,//tự động save sau khi kết thúc request (sẽ không hoạt động nếu sử dụng `exit` hoặc `die`)
'force_save' => false,//bắt buộc gọi save dù không thực hiện thao tác add, set, remove
'system_cnf' => false,//get config của app nếu trong setting không tồn tại
];
使用
更改别名
默认包会添加别名 Setting
,您只需使用此别名即可。如果需要更改别名,则将 'auto_alias'=>false
并在 config/app.php
的 aliases
中添加。
'aliases' => [
//.....
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
//.....
'YourAlias' => \Buzz\LaravelSettingFacade::class,
],
关闭自动保存
默认情况下,包仅在请求结束时并且设置有任何更改时保存。如果想要关闭此功能,则将 'auto_save' => true
并使用 Setting::save()
(或 YourAlias::save()
如果已更改别名)。**注意:自动保存功能在使用 exit
或 die
函数时将不会工作**。
支持函数
Setting::clean();//xóa tất cả settings Setting::save($force = false);//Lưu tất cả các thay đổi Setting::all();//Lấy ra tất cả settings Setting::has($key);//kiểm tra sự tồn tại của setting theo key Setting::get($key, $default = false, $system = false);//lấy giá trị theo key Setting::setData($data);//ghi đè tất cả settings Setting::add($key, $value);//thêm mới một setting Setting::add([ ['name' => 'name1', 'value' => 'value1'], ['name' => 'name2', 'value' => 'value2'], ['name' => 'name3', 'value' => 'value3'], ]);//thêm mới nhiều settings Setting::remove($key);//xóa một setting theo key Setting::remove([ 'name1', 'name2', 'name3', ]);//xóa nhiều settings theo key Setting::set($key, $default);//cập nhật giá trị mới cho setting theo key Setting::set([ ['name' => 'name1', 'value' => 'value1'], ['name' => 'name2', 'value' => 'value2'], ['name' => 'name3', 'value' => 'value3'], ]);//cập nhật giá trị mới cho nhiều settings theo key Setting::sync();//thêm tất cả config của app vào Setting Setting::sync('mail');//thêm config của app vào Setting theo key Setting::sync([ 'key1', 'key2', 'key3', ]);//Thêm nhiều config của app vào Setting theo key
包使用laravel的数组辅助函数。要更清楚地了解
$key
的使用,请参阅Laravel 辅助函数文档。