taylornetwork/setting 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

taylornetwork/setting

Composer 安装命令:

composer require taylornetwork/setting

包简介

A Laravel setting manager per user

README 文档

README

This package provides an easy way to access settings for the current logged in user as well as global app settings.

NOTE: Some features have changed in v2

  1. Changes
  2. About
  3. Install
  4. Usage
  5. HasSettings Trait
  6. Config
  7. Alternate Configuration
  8. Extending
  9. License

Changes

v2.1

  • Added support for float type values.

v2.0

Support for app specific AND user specific settings has been added.

  • Added AppSetting model and facade
  • Added app_setting helper
  • Added user_setting helper
  • Renamed Setting model and facade to UserSetting
  • Deprecated setting helper
  • Renamed config key setting_model to user_setting_model

If you use the setting helper it will still work but will be removed, so use user_setting instead. The Setting facade has been renamed to UserSetting if you still have code with the Setting facade you can rename the alias in your config/app.php

'aliases' => [
	...
	'Setting' => TaylorNetwork\Setting\Facades\UserSetting::class,
	...
],

About

This package uses Laravel's auth function to access the logged in user and return their setting for a given key, or a default value. Similar to the way that the config and env functions work.

When using the HasSettings trait it allows you to access a non-logged in user's settings.

By default if the default value is not set and a setting cannot be found, null is returned. The default value will also be returned if there is no logged in user.

It also includes support for app specific settings via AppSetting

Install

Via Composer

$ composer require taylornetwork/setting

Run database migrations

$ php artisan migrate

You can optionally add the HasSettings trait to your user model to access settings directly.

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use TaylorNetwork\Setting\Traits\HasSettings;

class User extends Authenticatable
{
   use HasSettings;
   
   // ...
}

Usage

Note: the UserSetting and AppSetting classes extend Illuminate\Database\Eloquent\Model

Available Methods

All of these methods are available when using the class or facade.

guard($guard) [UserSetting only]

This will allow you to manually set an auth guard before trying to find an active user.

UserSetting::guard('api')->get('key', 'defaultValue');

get($key, $default = null) [UserSetting and AppSetting]

Get a setting for the app/logged in user or return the default value.

UserSetting::get('key');

AppSetting::get('key');

set($key, $value) [UserSetting and AppSetting]

Creates or updates the setting for the logged in user or app.

UserSetting::set('someKey', 'someValue');

AppSetting::set('someKey', 'someValue');

Classes

UserSetting:

use TaylorNetwork\Setting\UserSetting;

$setting = new UserSetting;
$setting->get('someKey', 'defaultValue');

AppSetting:

use TaylorNetwork\Setting\AppSetting;

$setting = new AppSetting;
$setting->get('someKey', 'defaultValue');

Facades

UserSetting:

UserSetting::get('someKey');

UserSetting::set('someKey', 'someValue');

UserSetting::guard('api')->get('someKey', 'Default Value!');

AppSetting:

AppSetting::get('someKey');

AppSetting::set('someKey', 'someValue');

Helpers

The user_setting and app_setting helper functions are aliases for the get method. The user_setting helper has an optional third parameter to pass a guard or guards.

Example:

// Returns the user's value or null
user_setting('key');

// Returns the app's value or null
app_setting('key');

// Returns the user's value or 'defaultValue'
user_setting('key', 'defaultValue');

// Returns the app's value or 'defaultValue'
app_setting('key', 'defaultValue');

// Returns the user's value or 'defaultValue' using the 'api' guard
user_setting('key', 'defaultValue', 'api');

// Will try and get a value from each guard in order
// If a value other than the default value is found it will immediately return it.
// If none is found after using all the guards in the array, the default value is returned.
user_setting('key', 'defaultValue', ['web', 'api']);

HasSettings Trait

The HasSettings trait adds a settings relation and the following methods to your model

setting($key, $default = null)

This is the same as the get method above BUT can be used for any user not just one that is logged in.

updateSetting($key, $value)

This will create or update a setting for the specified user.

Config

If you need to change the default auth guard or user model, publish config.

$ php artisan vendor:publish --provider="TaylorNetwork\Setting\SettingServiceProvider" --tag=config

Alternate Configuration

You can modify the settings table to suit your needs, publish the migrations

$ php artisan vendor:publish --provider="TaylorNetwork\Setting\SettingServiceProvider" --tag=migrations

Be sure to update the config with appropriate related_column if you change it.

Extending

You can extend the UserSetting and AppSetting models if you need additional functionality.

namespace App;

use TaylorNetwork\Setting\UserSetting;

class Setting extends UserSetting
{
	// --
}

Be sure to update the config with the appropriate new user_setting_model or app_setting_model.

License

The MIT License (MIT). Please see License File for more information.

taylornetwork/setting 适用场景与选型建议

taylornetwork/setting 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 170 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 11 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 taylornetwork/setting 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 taylornetwork/setting 我们能提供哪些服务?
定制开发 / 二次开发

基于 taylornetwork/setting 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 170
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-11-16