cmarfil/laravel-multiuser-json-settings
Composer 安装命令:
composer require cmarfil/laravel-multiuser-json-settings
包简介
Simple user json settings facade for Laravel 4.
README 文档
README
Simple user settings facade for Laravel 4. Settings are stored as JSON in a single database column, so you can easily add it to an existing table (users for example).
Installation
- Begin by installing this package through Composer. Edit your project's composer.json file to require cmarfil/laravel-multiuser-json-settings.
"require": { "cmarfil/laravel-multiuser-json-settings": "~1.1" } - Add
'Cmarfil\LaravelMultiUserJsonSettings\ServiceProvider'toprovidersinapp/config/app.php.
'providers' => array( // ... 'Cmarfil\LaravelMultiUserJsonSettings\ServiceProvider', ),
- Add
'Setting' => 'Cmarfil\LaravelMultiUserJsonSettings\Facade'toaliasesinapp/config/app.php.
'aliases' => array( // ... 'Setting' => 'Cmarfil\LaravelMultiUserJsonSettings\Facade', ),
- Run
php artisan config:publish cmarfil/laravel-multiuser-json-settingsto publish the config file. - Modify the published configuration file located at
app/config/packages/cmarfil/laravel-multiuser-json-settings/config.phpto your liking. - Create a varchar (string) column in a table on your database to match the config file in step 5. Alternatively, use the Laravel migration included in this package to automatically create a
settingscolumn in theuserstable:php artisan migrate --package=cmarfil/laravel-multiuser-json-settings.
Configuration
Pop open app/config/packages/cmarfil/laravel-multiuser-json-settings/config.php to adjust package configuration. If this file doesn't exist, run php artisan config:publish cmarfil/laravel-multiuser-json-settings to create the default configuration file.
return array( 'table' => 'users', 'column' => 'settings', 'constraint_key' => 'id', 'default_constraint_value' => (Auth::check() ? Auth::id() : null) 'custom_constraint' => false, //'id = ' . (Auth::check() ? Auth::id() : null), );
Table
Specify the table on your database that you want to use.
Column
Specify the column in the above table that you want to store the settings JSON data in.
Constraint key
Specify the column constraint - this is used to differentiate between different users, objects or models ( normally id ).
Default constraint value
Specify the default constraint value - If you do not specify one, default configuration is obtained, in this case the user logged.
Custom constraint
Specify a where clause for each query - Caution: Leave blank if your want to set or get different rows on same runtime, use constraint_key and default_constraint_value
Usage
Use the Setting facade (Setting::) to access the functions in this package.
Set
Setting::set('key', 'value', $constraint_value);
Use set to change the value of a setting. If the setting does not exist, it will be created automatically. You can set multiple keys at once by passing an associative (key=>value) array to the first parameter.
If you do not pass constraint_value the value used by default is default_constraint_value
Get
Setting::get('key', 'default', $constraint_value);
Use get to retrieve the value of a setting. The second parameter is optional and can be used to specify a default value if the setting does not exist (the default default value is null).
If you do not pass constraint_value the value used by default is default_constraint_value
Forget
Setting::forget('key', $constraint_value);
Unset or delete a setting by calling forget.
If you do not pass constraint_value the value used by default is default_constraint_value
Has
Setting::has('key', $constraint_value);
Check for the existence of a setting, returned as a boolean. If you do not pass constraint_value the value used by default is default_constraint_value
All
Setting::all($constraint_value);
Retrieve all settings as an associative array (key=>value). If you do not pass constraint_value the value used by default is default_constraint_value
Save
Setting::save($constraint_value);
Save all changes back to the database. This will need to be called after making changes; it is not automatic. If you do not pass constraint_value the value used by default is default_constraint_value
Load
Setting::load($constraint_value);
Reload settings from the database. This is called automatically if settings have not been loaded before being accessed or mutated. If you do not pass constraint_value the value used by default is default_constraint_value
##Example With default configuration:
return array( 'table' => 'users', 'column' => 'settings', 'constraint_key' => 'id', 'default_constraint_value' => (Auth::check() ? Auth::id() : null) 'custom_constraint' => false, //'id = ' . (Auth::check() ? Auth::id() : null), );
The following set and returns the user logged setting "email_notification"
//Set email_notifications setting to false Setting::set('email_notifications', false); //Save config Setting::save(); //Save email_notifications return Setting::get('email_notifications');
The following set and returns the setting "email_notification" for user with id 23
//Set email_notifications setting to false Setting::set('email_notifications', false, 23); //Save config Setting::save(23); //Save email_notifications return Setting::get('email_notifications', true, 23);
Finally
Contributing
Feel free to create a fork and submit a pull request if you would like to contribute.
Bug reports
Raise an issue on GitHub if you notice something broken.
Credits
Fork of https://github.com/Grimthorr/laravel-user-settings. Based loosely on https://github.com/anlutro/laravel-settings.
cmarfil/laravel-multiuser-json-settings 适用场景与选型建议
cmarfil/laravel-multiuser-json-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 275 次下载、GitHub Stars 达 5, 最近一次更新时间为 2015 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「user」 「laravel」 「Settings」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cmarfil/laravel-multiuser-json-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cmarfil/laravel-multiuser-json-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cmarfil/laravel-multiuser-json-settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
Get wordpress nav menus and share the main site menus with the child sites.
Standalone replacement for php's native get_browser() function
统计信息
- 总下载量: 275
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-02-17