drey/prefs 问题修复 & 功能扩展

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

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

drey/prefs

Composer 安装命令:

composer require drey/prefs

包简介

Store and retrieve user preferences

README 文档

README

Store and retrieve user preferences.

  1. Remember form default values according to the last submit (for instance, repeat the date limits or the selection of an account in a dropdown list, according to the last search).
  2. Customize the look and feel of a website.
  3. etc.

Usage

Database in file system

    use drey\Prefs\Factory;

    # Obtain a Prefs object
    # with file system storage and username
    $prefs = Factory::fileSystem('/path/to/directory','bob') 

    # set preference "color" for current user (bob)
    $prefs->set('color','red');
    
    # get color preference of current user
    $color = $prefs->get('color');
    
    # $color is set to 'red'

Database in a RDMB

Change the DB medium accordingly, using a pdo factory.

    use drey\Prefs\Factory;

    # obtain a PDO object
    $pdo = myConnect();

    # Obtain a Prefs object
    # with RDBM storage and username
    $prefs = Factory::pdo($pdo,'bob') 
 
    # set preference "color" for current user (bob)
    $prefs->set('color','red');
    
    # get color preference of current user
    $color = $prefs->get('color');
    
    # $color is set to 'red'

The table schema for MySQL should be (see directory sql/):

        CREATE TABLE `prefs`  (
          `name` varchar(255) NOT NULL,
          `value` varchar(255) NOT NULL,
          `username` varchar(32) NOT NULL,
          PRIMARY KEY (`name`,`username`),
          KEY (`username`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Default values

If key is not found, a default value can be provided.

    $bird = $prefs->get('bird','eagle');
    
    # $bird is set to 'eagle' if key 'bird' not found

Specify preferences for other users

To assign/read the preferences of other users:

    $prefs->set('fruit','lemmon','alice');
    # ...
    $prefs->get('fruit','pear','alice')

Global preferences

A global username could be used to handle preferences for all users. All users must agree on the global username (for instance '*').

    # one user
    $prefs->set('closing_date',$form->closing_date,'*');

    (...)

    # other users
    $closing_date = $prefs->get('closing_date',date('Y-m-d'),'*');
   
 

Populate form fields:

Populate fields with default values if a field in our model has not any value and we are creating a new record.

    # before showing the form get the last value entered
    if ($model->isNewRecord && !$model->date) {
        $model->date = $prefs->get('invoice_date',date('Y-m-d'));
    }

    # after post, update value entered
    if (post) {
        $prefs->set('invoice_date',$model->date);
    }

Installing with composer

The package exists in Packagist repository as drey/prefs.

See drey/prefs.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-05-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固