judahnator/laravel-option

A simple option package for Laravel

v0.4.1 2018-06-15 16:09 UTC

README

pipeline status

This is a simple package to provide a key=>value option store for Laravel.

This package was inspired by the WordPress get_option() functionality. If you are interested in building a non-laravel implementation, check out my judahnator/option package. A simple key/value store to keep basic site settings persistent, especially settings that change too frequently to add to your env but that do not justify having a whole application model for.

Installation

Installation is fairly straightforward. Simply require this package via composer.

composer require judahnator/laravel-option

You will also need to publish some assets. Run the php artisan vendor:publish command and follow the instructions there.

Usage

Usage is super straightforward.

  • To get an option:
    • \Option::get('foo', 'optional default value if foo not found')
  • To check if an option exists:
    • \Option::has('foo')
  • To set (or overwrite) an option:
    • \Option::set('foo', 'foos value')
  • To delete an option:
    • \Option::delete('foo')

For convenience sake I have also added some helper functions. They are just aliases of the above, but you are free to use them as well.

  • delete_option($key)
  • get_option($key, $default)
  • has_option($key)
  • set_option($key, $value)

Custom Drivers

If you do not like any of the default drivers and want to use your own, no problem! The method of doing so is fairly straightforward.

Create your "driver," which is a class that implements the \judahnator\Option\OptionInterface interface. That interface defines all of the functions required. If you are looking for a starting point, you can extend the \judahnator\LaravelOption\Drivers\MemoryDriver class. Check out the \judahnator\LaravelOption\Drivers\JsonFileDriver for an example of how to do just that.

Once you have your driver written, you need to add it to your config file. For the "driver" option add the full path to your class. For example:

[
    // ...
    
    'driver' => \Path\To\Custom\Driver::class,
    
    // ...
]