PHPackages                             pozitronik/yii2-options - 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. pozitronik/yii2-options

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

pozitronik/yii2-options
=======================

Server-side options storage

2.0.2(5mo ago)23.2k1[1 issues](https://github.com/pozitronik/yii2-options/issues)[1 PRs](https://github.com/pozitronik/yii2-options/pulls)GPL-3.0PHPPHP &gt;=8.4CI passing

Since Jan 12Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/pozitronik/yii2-options)[ Packagist](https://packagist.org/packages/pozitronik/yii2-options)[ RSS](/packages/pozitronik-yii2-options/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (7)Versions (13)Used By (0)

yii2-options
============

[](#yii2-options)

[![Tests](https://github.com/pozitronik/yii2-options/actions/workflows/ci.yml/badge.svg)](https://github.com/pozitronik/yii2-options/actions/workflows/ci.yml)[![Codecov](https://camo.githubusercontent.com/783173a16d6e7f36d5c1097af363d020abbac04f0e58d6dc2d2976448f3b0f2c/68747470733a2f2f636f6465636f762e696f2f67682f706f7a6974726f6e696b2f796969322d6f7074696f6e732f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/pozitronik/yii2-options)[![Packagist Version](https://camo.githubusercontent.com/fab8b15f5beda79aa7f0245a55af7bbf22ae8db297ffcc93b710731b936f8167/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f706f7a6974726f6e696b2f796969322d6f7074696f6e73)](https://packagist.org/packages/pozitronik/yii2-options)[![Packagist License](https://camo.githubusercontent.com/ec24a1404bf3b7e40ba594bca02acf7c8f7696db481ac2adbe8622f03f375b88/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f706f7a6974726f6e696b2f796969322d6f7074696f6e73)](https://packagist.org/packages/pozitronik/yii2-options)[![Packagist Downloads](https://camo.githubusercontent.com/1ded719ba3f4ab14d873f6860d3eb8e565b2edca843b8afd49c85394059f047d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f706f7a6974726f6e696b2f796969322d6f7074696f6e73)](https://packagist.org/packages/pozitronik/yii2-options)

Server-side key-value options storage for Yii2 applications.

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

[](#installation)

Install via [Composer](http://getcomposer.org/download/):

```
composer require pozitronik/yii2-options ^2.0.0
```

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

[](#quick-start)

1. Run database migration to create the options table:

```
./yii migrate --migrationPath=@vendor/pozitronik/yii2-options/migrations
```

2. Configure the component in your application config:

```
'components' => [
    'sysoptions' => [
        'class' => \pozitronik\sys_options\models\SysOptions::class,
    ],
],
```

3. Use it in your code:

```
// Set an option
SysOptions::setStatic('app.theme', 'dark');

// Get an option
$theme = SysOptions::getStatic('app.theme', 'light'); // Returns 'dark'

// Delete an option
SysOptions::dropStatic('app.theme');
```

Configuration
-------------

[](#configuration)

Configure the component with optional parameters:

```
'components' => [
    'sysoptions' => [
        'class' => \pozitronik\sys_options\models\SysOptions::class,
        'tableName' => 'custom_options',      // Default: 'sys_options'
        'cache' => 'cache',                    // Default: 'cache' (set to null to disable)
        'cacheDuration' => 3600,               // Default: null (infinite)
        'allowedClasses' => false,             // Default: true (see Security below)
    ],
],
```

### Configuration Options

[](#configuration-options)

PropertyTypeDefaultDescription`tableName``string``'sys_options'`Database table name for storing options`cache``string|CacheInterface|null``'cache'`Cache component ID or instance (null to disable caching)`cachePrefix``string|null``null`Cache key prefix for namespace isolation (uses class name)`cacheDuration``int|null``null`Cache duration in seconds (null = infinite)`db``string|Connection``'db'`Database connection component ID or instance`allowedClasses``bool|array``true`Classes allowed for deserialization (see Security)`serializer``array|null``null`Custom serialization functions`legacyCacheCompatibility``bool``false`Enable v1.1.0 cache fix during migration (temporary)Usage
-----

[](#usage)

### Instance Methods

[](#instance-methods)

```
$options = Yii::$app->sysoptions;

// Set option
$options->set('user.notifications', true);

// Get option with default fallback
$notifications = $options->get('user.notifications', false);

// Check null vs non-existent
$options->set('explicit.null', null);
$options->get('explicit.null');    // Returns: null (exists in DB)
$options->get('nonexistent');      // Returns: null (doesn't exist)

// Delete option
$options->drop('user.notifications');

// Bulk operations
$all = $options->retrieveOptions();                  // Get all options
$names = $options->getAllNames();                    // Get all option names
$appOptions = $options->getByPattern('app.%');       // Get by SQL LIKE pattern
$options->clear();                                   // Delete all options
```

### Static Methods

[](#static-methods)

For convenience, you can use static methods without accessing the component:

```
use pozitronik\sys_options\models\SysOptions;

SysOptions::setStatic('config.version', '2.0');
$version = SysOptions::getStatic('config.version');
SysOptions::dropStatic('config.version');
```

**Note:** Static methods require the `sysoptions` component to be configured in your application.

Data Types
----------

[](#data-types)

The extension uses PHP serialization by default and supports any serializable data type:

```
// Scalars
$options->set('string', 'value');
$options->set('integer', 42);
$options->set('float', 3.14);
$options->set('boolean', true);
$options->set('null', null);

// Arrays
$options->set('array', ['key' => 'value', 'nested' => ['data']]);

// Objects (when allowedClasses permits)
$options->set('datetime', new DateTime());
```

### Custom Serialization

[](#custom-serialization)

You can use custom serialization (e.g., JSON):

```
$options->serializer = [
    fn($value) => json_encode($value),           // Serialize
    fn(string $value) => json_decode($value),    // Deserialize
];
```

Security
--------

[](#security)

The `allowedClasses` parameter controls PHP object deserialization security:

```
// RECOMMENDED: Only primitives (no objects)
'allowedClasses' => false,

// Whitelist specific classes
'allowedClasses' => [stdClass::class, DateTime::class],

// Allow all classes (default for backward compatibility - NOT RECOMMENDED)
'allowedClasses' => true,
```

**Important:** Setting `allowedClasses` to `true` may pose security risks if your database is compromised. See [PHP Object Injection](https://owasp.org/www-community/vulnerabilities/PHP_Object_Injection) for details.

Caching
-------

[](#caching)

The extension uses Yii2's caching with TagDependency for automatic cache invalidation:

```
'components' => [
    'sysoptions' => [
        'class' => \pozitronik\sys_options\models\SysOptions::class,
        'cache' => 'cache',          // Cache component (set to null to disable)
        'cacheDuration' => 3600,     // 1 hour (null = infinite)
    ],
],
```

Cache is automatically invalidated when options are modified.

Multiple Instances with Separate Cache Namespaces
-------------------------------------------------

[](#multiple-instances-with-separate-cache-namespaces)

When using multiple SysOptions instances (e.g., for different tables or applications), you can use `cachePrefix` to prevent cache key collisions:

```
'components' => [
    // Application options
    'appOptions' => [
        'class' => \pozitronik\sys_options\models\SysOptions::class,
        'tableName' => 'app_options',
        'cachePrefix' => 'AppOptions',     // Custom cache prefix
    ],

    // User options
    'userOptions' => [
        'class' => \pozitronik\sys_options\models\SysOptions::class,
        'tableName' => 'user_options',
        'cachePrefix' => 'UserOptions',    // Different cache prefix
    ],
],
```

Each instance will use its own cache namespace, preventing interference:

- `AppOptions::get(theme)` → Cache key: `AppOptions::get(theme)`
- `UserOptions::get(theme)` → Cache key: `UserOptions::get(theme)`

Without `cachePrefix`, both would use the same cache key based on the class name, causing cache collisions.

Migrating from v1.x to v2.x
---------------------------

[](#migrating-from-v1x-to-v2x)

When upgrading from v1.x to v2.x without flushing cache, you may encounter issues where non-existent options return `null` instead of default values. This is caused by legacy cache entries from v1.1.0.

### Option 1: Enable Legacy Cache Compatibility (Recommended for Zero-Downtime)

[](#option-1-enable-legacy-cache-compatibility-recommended-for-zero-downtime)

Enable the compatibility fix temporarily during migration:

```
'components' => [
    'sysoptions' => [
        'class' => \pozitronik\sys_options\models\SysOptions::class,
        'legacyCacheCompatibility' => true,  // Enable during migration
    ],
],
```

This will automatically detect and fix v1.1.0 cache entries on first access. Once all legacy cache entries expire (based on your cache TTL) or after a reasonable migration period, disable this option to avoid the extra database query overhead.

### Option 2: Flush Cache (Clean Approach)

[](#option-2-flush-cache-clean-approach)

If you can afford to flush the cache during deployment:

```
# Flush all cache
yii cache/flush-all

# Or flush programmatically
Yii::$app->cache->flush();
```

License
-------

[](#license)

GNU GPL v3.0

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance63

Regular maintenance activity

Popularity21

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 98.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~176 days

Recently: every ~2 days

Total

9

Last Release

170d ago

Major Versions

1.1.0 → 2.0.0-rc.12025-11-13

PHP version history (2 changes)1.0.0PHP &gt;=8.0

2.0.0-rc.1PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/76ddc43524bb32eb36568e25b6ae283bc25bb51a4572789997909092145af0f5?d=identicon)[pozitronik](/maintainers/pozitronik)

---

Top Contributors

[![pozitronik](https://avatars.githubusercontent.com/u/2357892?v=4)](https://github.com/pozitronik "pozitronik (84 commits)")[![s1lver](https://avatars.githubusercontent.com/u/4567634?v=4)](https://github.com/s1lver "s1lver (1 commits)")

---

Tags

yii2extension

###  Code Quality

TestsCodeception

### Embed Badge

![Health badge](/badges/pozitronik-yii2-options/health.svg)

```
[![Health](https://phpackages.com/badges/pozitronik-yii2-options/health.svg)](https://phpackages.com/packages/pozitronik-yii2-options)
```

###  Alternatives

[vyants/yii2-daemon

Extension provides functionality for simple daemons creation and control

7859.0k](/packages/vyants-yii2-daemon)[richardfan1126/yii2-js-register

Yii2 widget to register JS into view

1357.2k7](/packages/richardfan1126-yii2-js-register)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
