Skip to content

Commit a12c52c

Browse files
committed
Services and “configuration”
1 parent 4ab7962 commit a12c52c

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

docs/6.x/extend/services.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,30 @@ The same pattern could be applied to widgets (`CraftCms\Cms\Dashboard\Dashboard:
158158
To make the transition a bit easier, we’ve brought along some of Yii’s “component” features that were upstream of Craft services.
159159
Your services (and other data models) can extend `CraftCms\Cms\Component\Component` to get access to [validation](validation.md), macros, array-style access, and more.
160160

161+
### Configuration
162+
163+
Laravel is broadly less config-driven than Yii.
164+
Services can be instantiated from anywhere (and at any time), so it’s expected that they know how to resolve their configuration, internally (usually by grabbing values with `config(...)` at runtime).
165+
As a result of flattening the application’s overall architecture, Yii’s [application configuration](guide:structure-applications#application-configurations) is no longer relevant—there is no centrally-defined component “tree” to initialize from config.
166+
167+
Components bridge this gap by accepting a “configuration” array in their constructors, which is mapped and typecast onto its public properties.
168+
In many cases, we have elected to replace this top-down pattern with a combination of other strategies:
169+
170+
- Initialize with defaults that draw from general config settings and other `config(...)` calls;
171+
- Provide methods to dynamically or temporarily reconfigure a service for the duration of a closure;
172+
173+
You may have already been exposed to the second strategy when rendering templates, in earlier versions of Craft:
174+
175+
```php
176+
use CraftCms\Cms\View\TemplateMode;
177+
178+
TemplateMode::with(TemplateMode::Site, function () {
179+
// ...
180+
});
181+
182+
// Previously: Craft::$app->getView()->renderTemplate(sprintf('forms/%s', $form->handle), View::TEMPLATE_MODE_SITE);
183+
```
184+
161185
## Plugin Getters
162186

163187
Because your services are all accessible by their class names, plugins no longer need to call `setComponents([])`, or implement “getter” methods.

0 commit comments

Comments
 (0)