PHPackages                             tatter/preferences - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tatter/preferences

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tatter/preferences
==================

Persistent user-specific settings for CodeIgniter 4

v1.0.0-rc.4(4y ago)745.2k↓50%2[1 PRs](https://github.com/tattersoftware/codeigniter4-preferences/pulls)MITPHPPHP ^7.3 || ^8.0

Since Nov 19Pushed 1y ago5 watchersCompare

[ Source](https://github.com/tattersoftware/codeigniter4-preferences)[ Packagist](https://packagist.org/packages/tatter/preferences)[ Docs](https://github.com/tattersoftware/codeigniter4-preferences)[ Fund](https://paypal.me/tatter)[ GitHub Sponsors](https://github.com/tattersoftware)[ RSS](/packages/tatter-preferences/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (4)Dependencies (4)Versions (7)Used By (0)

Tatter\\Preferences
===================

[](#tatterpreferences)

Persistent user-specific settings for CodeIgniter 4

[![](https://github.com/tattersoftware/codeigniter4-preferences/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-preferences/actions/workflows/test.yml)[![](https://github.com/tattersoftware/codeigniter4-preferences/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-preferences/actions/workflows/analyze.yml)[![](https://github.com/tattersoftware/codeigniter4-preferences/workflows/Deptrac/badge.svg)](https://github.com/tattersoftware/codeigniter4-preferences/actions/workflows/inspect.yml)[![Coverage Status](https://camo.githubusercontent.com/92b72f1fcb2ea40a279cc1b729a8b9a6cfcaa40099550ac19163f6e0e0d40d18/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f746174746572736f6674776172652f636f646569676e69746572342d707265666572656e6365732f62616467652e7376673f6272616e63683d646576656c6f70)](https://coveralls.io/github/tattersoftware/codeigniter4-preferences?branch=develop)

Quick Start
-----------

[](#quick-start)

1. Install with Composer: `> composer require --dev tatter/preferences`
2. Load the helper: `helper('preferences');`
3. Use the function to get and set: `$theme = preference('theme'); preference('theme', 'dark');`

Description
-----------

[](#description)

`Preferences` is a wrapper for [CodeIgniter Settings](https://github.com/codeigniter4/settings)to provide user context to each setting. This allows you to get and set preferences on a per-user basis with a single command.

Installation
------------

[](#installation)

Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities and always be up-to-date:

```
composer require tatter/preferences
```

Or, install manually by downloading the source files and adding the directory to `app/Config/Autoload.php`.

Once the files are downloaded and included in the autoload, run any library migrations to ensure the database is set up correctly:

- `> php spark migrate -all`

`Preferences` suggests the Composer provision for `codeigniter4/authentication-implementation` as describe in the [CodeIgniter authentication guidelines](https://codeigniter4.github.io/CodeIgniter4/extending/authentication.html). Without this each preference will be limited to the session duration so it is highly recommended you install and configure a [supported package](https://packagist.org/providers/codeigniter4/authentication-implementation).

Usage
-----

[](#usage)

`Preferences` requires [CodeIgniter Settings](https://github.com/codeigniter4/settings) so you may use all the same classes and functions described in its documentation as well. To access the user-specific context settings call the `preference()` function anywhere you would normally use `setting()`:

```
class Home extends Controller
{
    public function index()
    {
        return view('welcome', [
            'icon' => preference('Users.avatar'),
        ];
    }

    public function update_avatar()
    {
        if ($icon = $this->request->getPost('icon')) {
            preference('Users.avatar', $icon);
        }

        return redirect()->back();
    }
}
```

> Note: Be sure to load the helper file (`helper('preferences')`) before using the helper function.

`preference()` will retrieve and store contextual settings for the current authenticated user. If no user is authenticated then it will fall back on the `Session` class with semi-persistent settings for as long as the session lasts.

### Placeholder Config

[](#placeholder-config)

In most cases each setting should have a corresponding Config file. Sometimes these settings will not fit under an existing logical grouping, so this library provides a "placeholder" Config (`Tatter\Preferences\Config\Preferences`). You may add your own version in \**app/* to supply default values:

```
