@@ -80,6 +80,39 @@ it effectively resets itself back to the default value in config file, if any.
8080service('setting')->forget('App.siteName')
8181```
8282
83+ ### Contextual Settings
84+
85+ In addition to the default behavior describe above, ` Settings ` can can be used to define "contextual settings".
86+ A context may be anything you want, but common examples are a runtime environment or an authenticated user.
87+ In order to use a context you pass it as an additional parameter to the ` get() ` /` set() ` /` forget() ` methods; if
88+ a context setting is requested and does not exist then the general value will be used.
89+
90+ Contexts may be any unique string you choose, but a recommended format for supplying some consistency is to
91+ give them a category and identifier, like ` environment:production ` or ` group:42 ` .
92+
93+ An example... Say your App config includes the name of a theme to use to enhance your display. By default
94+ your config file specifies ` App.theme = 'default' ` . When a user changes their theme, you do not want this to
95+ change the theme for all visitors to the site, so you need to provide the user as the * context* for the change:
96+
97+ ``` php
98+ $context = 'user:' . user_id();
99+ service('setting')->set('App.theme', 'dark', $context);
100+ ```
101+
102+ Now when your filter is determining which theme to apply it can check for the current user as the context:
103+
104+ ``` php
105+ $context = 'user:' . user_id();
106+ $theme = service('setting')->get('App.theme', $context);
107+
108+ // or using the helper
109+ setting()->get('App.theme', $context);
110+ ```
111+
112+ Contexts are a cascading check, so if a context does not match a value it will fall back on general,
113+ i.e. ` service('setting')->get('App.theme') ` . Return value priority is as follows:
114+ "Setting with a context > Setting without context > Config value > null".
115+
83116### Using the Helper
84117
85118The helper provides a shortcut to the using the service. It must first be loaded using the ` helper() ` method
@@ -100,6 +133,8 @@ setting()->set('App.siteName', 'My Great Site');
100133setting()->forget('App.siteName');
101134```
102135
136+ > Note: Due to the shorthand nature of the helper function it cannot access contextual settings.
137+
103138## Known Limitations
104139
105140The following are known limitations of the library:
0 commit comments