Skip to content

Commit b9f36ca

Browse files
committed
better documentation
1 parent b40bb6f commit b9f36ca

File tree

2 files changed

+160
-17
lines changed

2 files changed

+160
-17
lines changed

README.md

Lines changed: 153 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,231 @@
1-
# Settings for your App
1+
# Laravel Settings
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/elegantly/laravel-settings.svg?style=flat-square)](https://packagist.org/packages/elegantly/laravel-settings)
4-
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/elegantly/laravel-settings/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/ElegantEngineeringTech/laravel-settings/actions?query=workflow%3Arun-tests+branch%3Amain)
5-
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/elegantly/laravel-settings/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/ElegantEngineeringTech/laravel-settings/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
4+
[![Tests](https://img.shields.io/github/actions/workflow/status/ElegantEngineeringTech/laravel-settings/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/ElegantEngineeringTech/laravel-settings/actions?query=workflow%3Arun-tests+branch%3Amain)
5+
[![Code Style](https://img.shields.io/github/actions/workflow/status/ElegantEngineeringTech/laravel-settings/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/ElegantEngineeringTech/laravel-settings/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
6+
[![PHPStan Level](https://img.shields.io/github/actions/workflow/status/ElegantEngineeringTech/laravel-settings/phpstan.yml?label=phpstan&style=flat-square)](https://github.com/ElegantEngineeringTech/laravel-settings/actions?query=workflow%3Aphpstan)
7+
[![Laravel Pint](https://img.shields.io/github/actions/workflow/status/ElegantEngineeringTech/laravel-settings/pint.yml?label=laravel%20pint&style=flat-square)](https://github.com/ElegantEngineeringTech/laravel-settings/actions?query=workflow%3Apint)
68
[![Total Downloads](https://img.shields.io/packagist/dt/elegantly/laravel-settings.svg?style=flat-square)](https://packagist.org/packages/elegantly/laravel-settings)
79

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+
---
942

1043
## Installation
1144

12-
You can install the package via composer:
45+
Install the package via Composer:
1346

1447
```bash
1548
composer require elegantly/laravel-settings
1649
```
1750

18-
You can publish and run the migrations with:
51+
Publish the migration and run it:
1952

2053
```bash
2154
php artisan vendor:publish --tag="settings-migrations"
2255
php artisan migrate
2356
```
2457

25-
You can publish the config file with:
58+
Optionally, publish the configuration file:
2659

2760
```bash
2861
php artisan vendor:publish --tag="settings-config"
2962
```
3063

31-
This is the contents of the published config file:
64+
---
65+
66+
## Configuration
67+
68+
Published config file (`config/settings.php`):
3269

3370
```php
3471
use Elegantly\Settings\Models\Setting;
3572

3673
return [
3774

75+
/*
76+
* The Eloquent model used to store and retrieve settings
77+
*/
3878
'model' => Setting::class,
3979

80+
/*
81+
* Cache configuration for global settings
82+
*/
4083
'cache' => [
4184
'enabled' => true,
4285
'key' => 'settings',
43-
'ttl' => 60 * 60 * 24,
86+
'ttl' => 60 * 60 * 24, // 1 day
4487
],
4588

4689
];
4790
```
4891

92+
---
93+
4994
## Usage
5095

96+
### Basic Usage (Facade)
97+
5198
```php
5299
use Elegantly\Settings\Facades\Settings;
53100

101+
// Set a value
54102
Settings::set(
55103
namespace: 'home',
56104
name: 'color',
57105
value: 'white'
58106
);
59107

108+
// Get a value
60109
$setting = Settings::get(
61110
namespace: 'home',
62-
name: 'color',
111+
name: 'color'
63112
);
64113

65-
echo $setting->value; // white
114+
$setting->value; // white
115+
```
116+
117+
### Dependency Injection
66118

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+
}
67142
```
68143

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+
69190
## Testing
70191

192+
Run the test suite:
193+
71194
```bash
72195
composer test
73196
```
74197

198+
---
199+
75200
## Changelog
76201

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+
---
78205

79206
## Contributing
80207

81-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
208+
See [CONTRIBUTING](CONTRIBUTING.md) for contribution guidelines.
209+
210+
---
82211

83212
## Security Vulnerabilities
84213

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+
---
86217

87218
## Credits
88219

89220
- [Quentin Gabriele](https://github.com/QuentinGab)
90221
- [All Contributors](../../contributors)
91222

92-
## License
223+
---
224+
225+
## 📝 License
226+
227+
This package is open-sourced under the [MIT license](LICENSE.md).
228+
229+
---
93230

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?

config/settings.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66

77
return [
88

9+
/*
10+
* The Eloquent model used to store and retrieve settings
11+
*/
912
'model' => Setting::class,
1013

14+
/*
15+
* Cache configuration for global settings
16+
*/
1117
'cache' => [
1218
'enabled' => true,
1319
'key' => 'settings',
14-
'ttl' => 60 * 60 * 24,
20+
'ttl' => 60 * 60 * 24, // 1 day
1521
],
1622

1723
];

0 commit comments

Comments
 (0)