arandu/laravel-site-options

There is no license information available for the latest version (v2.1.0) of this package.

v2.1.0 2023-12-05 20:33 UTC

This package is auto-updated.

Last update: 2024-05-05 21:37:01 UTC


README

Tests

Laravel Site Options is a lightweight package designed for Laravel applications to manage global site settings seamlessly. It provides a straightforward interface for storing and retrieving dictionary-oriented options from the database, with built-in caching capabilities for enhanced performance.

Installation

Install the package via composer:

composer require arandu/laravel-site-options

Publish the configuration and migration files:

php artisan vendor:publish --provider="Arandu\LaravelSiteOptions\SiteOptionsServiceProvider"

Execute the migration to set up the database table:

php artisan migrate

Basic Usage

The package provides a fluent, expressive API for managing site options:

use Arandu\LaravelSiteOptions\Option;

// Storing options
Option::set('welcome_message', 'Hello, World!');
Option::set('site_settings', ['theme' => 'dark', 'layout' => 'wide']);

// Retrieving options
echo Option::get('welcome_message');
$settings = Option::get('site_settings', $fallbackSettings);

// Checking existence
if (Option::has('maintenance_mode')) {
    // Perform action
}

// Removing options
Option::rm('outdated_option');

This package utilizes PHP serialization to store and retrieve options. Therefore, you could store any serializable data type. However, you should avoid storing objects, as they could cause subtle bugs that are hard to debug. Read more in the serialization section.

Application Examples

  • Feature Toggles: Manage feature flags for enabling or disabling application features dynamically.
  • Site Settings: Store and retrieve global site settings like site name, logo, etc.
  • Configurable Content: Store and retrieve content that can be configured by some administrator user, like the phone number on the footer, or how much time to wait before showing a popup.
  • And whatever else you can think of!

Configuration

Configure the package in config/site-options.php:

  • table: Name of the database table for storing options.
  • cache: Caching settings.
    • enabled: Toggle caching (true/false). Settable in .env as SITE_OPTIONS_CACHE_ENABLED.
    • key: Cache key for storing options.
    • ttl: Cache TTL in minutes. Settable in .env as SITE_OPTIONS_CACHE_TTL.
  • hard_defaults: Hard-coded default values for options, overridable in Option::get().

To adjust these settings, edit the config/site-options.php file. For environment-specific settings, use the .env file.

Versions

The following table relates Laravel Framework versions to the corresponding Laravel Site Options versions:

Laravel Framework Laravel Site Options
8.x 1.x
9.x 2.x
10.x 3.x (planned, dev branch available)

Testing

To run the tests, run the following command from the project folder:

composer test

Issues

If you discover any issues, please use the GitHub issue tracker.

Security

Please refer to SECURITY.md for more information.

Contributing

Contributions are welcome! Please read CONTRIBUTING.md for details.

License

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