|
1 |
| -# Settings for your App |
| 1 | +# Laravel Settings |
2 | 2 |
|
3 | 3 | [](https://packagist.org/packages/elegantly/laravel-settings)
|
4 |
| -[](https://github.com/ElegantEngineeringTech/laravel-settings/actions?query=workflow%3Arun-tests+branch%3Amain) |
5 |
| -[](https://github.com/ElegantEngineeringTech/laravel-settings/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) |
| 4 | +[](https://github.com/ElegantEngineeringTech/laravel-settings/actions?query=workflow%3Arun-tests+branch%3Amain) |
| 5 | +[](https://github.com/ElegantEngineeringTech/laravel-settings/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) |
| 6 | +[](https://github.com/ElegantEngineeringTech/laravel-settings/actions?query=workflow%3Aphpstan) |
| 7 | +[](https://github.com/ElegantEngineeringTech/laravel-settings/actions?query=workflow%3Apint) |
6 | 8 | [](https://packagist.org/packages/elegantly/laravel-settings)
|
7 | 9 |
|
8 |
| -Settings for your Laravel App. Done right. |
| 10 | +A simple and flexible way to manage global or model-specific settings in your Laravel app. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +- Define global settings |
| 17 | +- Store settings in the database via Eloquent |
| 18 | +- Cache settings for performance |
| 19 | +- Attach settings to users or any other model |
| 20 | +- Support for typed namespaced settings classes |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## Table of Contents |
| 25 | + |
| 26 | +- [Installation](#installation) |
| 27 | +- [Configuration](#configuration) |
| 28 | +- [Usage](#usage) |
| 29 | + |
| 30 | + - [Basic Usage (Facade)](#basic-usage-facade) |
| 31 | + - [Dependency Injection](#dependency-injection) |
| 32 | + - [Typed Namespaced Settings](#typed-namespaced-settings) |
| 33 | + |
| 34 | +- [Testing](#testing) |
| 35 | +- [Changelog](#changelog) |
| 36 | +- [Contributing](#contributing) |
| 37 | +- [Security](#security-vulnerabilities) |
| 38 | +- [Credits](#credits) |
| 39 | +- [License](#license) |
| 40 | + |
| 41 | +--- |
9 | 42 |
|
10 | 43 | ## Installation
|
11 | 44 |
|
12 |
| -You can install the package via composer: |
| 45 | +Install the package via Composer: |
13 | 46 |
|
14 | 47 | ```bash
|
15 | 48 | composer require elegantly/laravel-settings
|
16 | 49 | ```
|
17 | 50 |
|
18 |
| -You can publish and run the migrations with: |
| 51 | +Publish the migration and run it: |
19 | 52 |
|
20 | 53 | ```bash
|
21 | 54 | php artisan vendor:publish --tag="settings-migrations"
|
22 | 55 | php artisan migrate
|
23 | 56 | ```
|
24 | 57 |
|
25 |
| -You can publish the config file with: |
| 58 | +Optionally, publish the configuration file: |
26 | 59 |
|
27 | 60 | ```bash
|
28 | 61 | php artisan vendor:publish --tag="settings-config"
|
29 | 62 | ```
|
30 | 63 |
|
31 |
| -This is the contents of the published config file: |
| 64 | +--- |
| 65 | + |
| 66 | +## Configuration |
| 67 | + |
| 68 | +Published config file (`config/settings.php`): |
32 | 69 |
|
33 | 70 | ```php
|
34 | 71 | use Elegantly\Settings\Models\Setting;
|
35 | 72 |
|
36 | 73 | return [
|
37 | 74 |
|
| 75 | + /* |
| 76 | + * The Eloquent model used to store and retrieve settings |
| 77 | + */ |
38 | 78 | 'model' => Setting::class,
|
39 | 79 |
|
| 80 | + /* |
| 81 | + * Cache configuration for global settings |
| 82 | + */ |
40 | 83 | 'cache' => [
|
41 | 84 | 'enabled' => true,
|
42 | 85 | 'key' => 'settings',
|
43 |
| - 'ttl' => 60 * 60 * 24, |
| 86 | + 'ttl' => 60 * 60 * 24, // 1 day |
44 | 87 | ],
|
45 | 88 |
|
46 | 89 | ];
|
47 | 90 | ```
|
48 | 91 |
|
| 92 | +--- |
| 93 | + |
49 | 94 | ## Usage
|
50 | 95 |
|
| 96 | +### Basic Usage (Facade) |
| 97 | + |
51 | 98 | ```php
|
52 | 99 | use Elegantly\Settings\Facades\Settings;
|
53 | 100 |
|
| 101 | +// Set a value |
54 | 102 | Settings::set(
|
55 | 103 | namespace: 'home',
|
56 | 104 | name: 'color',
|
57 | 105 | value: 'white'
|
58 | 106 | );
|
59 | 107 |
|
| 108 | +// Get a value |
60 | 109 | $setting = Settings::get(
|
61 | 110 | namespace: 'home',
|
62 |
| - name: 'color', |
| 111 | + name: 'color' |
63 | 112 | );
|
64 | 113 |
|
65 |
| -echo $setting->value; // white |
| 114 | +$setting->value; // white |
| 115 | +``` |
| 116 | + |
| 117 | +### Dependency Injection |
66 | 118 |
|
| 119 | +```php |
| 120 | +namespace App\Http\Controllers; |
| 121 | + |
| 122 | +use Elegantly\Settings\Settings; |
| 123 | + |
| 124 | +class UserController extends Controller |
| 125 | +{ |
| 126 | + public function index(Settings $settings) |
| 127 | + { |
| 128 | + $settings->set( |
| 129 | + namespace: 'home', |
| 130 | + name: 'color', |
| 131 | + value: 'white' |
| 132 | + ); |
| 133 | + |
| 134 | + $setting = $settings->get( |
| 135 | + namespace: 'home', |
| 136 | + name: 'color' |
| 137 | + ); |
| 138 | + |
| 139 | + $setting->value; // white |
| 140 | + } |
| 141 | +} |
67 | 142 | ```
|
68 | 143 |
|
| 144 | +### Typed Namespaced Settings |
| 145 | + |
| 146 | +For better DX, define custom typed settings classes: |
| 147 | + |
| 148 | +#### Usage |
| 149 | + |
| 150 | +```php |
| 151 | +namespace App\Http\Controllers; |
| 152 | + |
| 153 | +use App\Settings\HomeSettings; |
| 154 | + |
| 155 | +class UserController extends Controller |
| 156 | +{ |
| 157 | + public function index(HomeSettings $settings) |
| 158 | + { |
| 159 | + $settings->color; // white |
| 160 | + |
| 161 | + $settings->color = 'black'; |
| 162 | + $settings->save(); |
| 163 | + } |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +#### Defining a Typed Settings Class |
| 168 | + |
| 169 | +```php |
| 170 | +namespace App\Settings; |
| 171 | + |
| 172 | +use Elegantly\Settings\NamespacedSettings; |
| 173 | + |
| 174 | +class HomeSettings extends NamespacedSettings |
| 175 | +{ |
| 176 | + public ?string $color = null; |
| 177 | + |
| 178 | + /** @var int[] */ |
| 179 | + public array $articles = []; |
| 180 | + |
| 181 | + public static function getNamespace(): string |
| 182 | + { |
| 183 | + return 'home'; |
| 184 | + } |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +--- |
| 189 | + |
69 | 190 | ## Testing
|
70 | 191 |
|
| 192 | +Run the test suite: |
| 193 | + |
71 | 194 | ```bash
|
72 | 195 | composer test
|
73 | 196 | ```
|
74 | 197 |
|
| 198 | +--- |
| 199 | + |
75 | 200 | ## Changelog
|
76 | 201 |
|
77 |
| -Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 202 | +See [CHANGELOG](CHANGELOG.md) for details on recent changes. |
| 203 | + |
| 204 | +--- |
78 | 205 |
|
79 | 206 | ## Contributing
|
80 | 207 |
|
81 |
| -Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 208 | +See [CONTRIBUTING](CONTRIBUTING.md) for contribution guidelines. |
| 209 | + |
| 210 | +--- |
82 | 211 |
|
83 | 212 | ## Security Vulnerabilities
|
84 | 213 |
|
85 |
| -Please review [our security policy](../../security/policy) on how to report security vulnerabilities. |
| 214 | +Please review our [security policy](../../security/policy) for reporting vulnerabilities. |
| 215 | + |
| 216 | +--- |
86 | 217 |
|
87 | 218 | ## Credits
|
88 | 219 |
|
89 | 220 | - [Quentin Gabriele](https://github.com/QuentinGab)
|
90 | 221 | - [All Contributors](../../contributors)
|
91 | 222 |
|
92 |
| -## License |
| 223 | +--- |
| 224 | + |
| 225 | +## 📝 License |
| 226 | + |
| 227 | +This package is open-sourced under the [MIT license](LICENSE.md). |
| 228 | + |
| 229 | +--- |
93 | 230 |
|
94 |
| -The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
| 231 | +Would you like a README badge for PHPStan or Laravel Pint added as well? |
0 commit comments