Skip to content

Commit fcbf244

Browse files
committed
Add note about DI on service creation
1 parent 3ba60c6 commit fcbf244

File tree

1 file changed

+15
-0
lines changed
  • pages/07.dependency-injection/04.adding-services

1 file changed

+15
-0
lines changed

pages/07.dependency-injection/04.adding-services/docs.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ class MapBuilderService implements ServicesProviderInterface
6262

6363
[notice=tip]You'll notice that we've added `use UserFrosting\Sprinkle\Site\GoogleMaps\MapBuilder;` to the top of the file. This means that we don't have to use the fully qualified class name (with the entire namespace) every time we want to refer to the `MapBuilder` class.[/notice]
6464

65+
If you need to pull in another service, for example the config to retrieve an API key, you can add them as the parameter, and the dependency injector will automatically pick it up.
66+
67+
```php
68+
...
69+
MapBuilder::class => function (Config $config) {
70+
$apiKey = $config['api.key'];
71+
72+
// Now, actually build the object
73+
$mapBuilder = new MapBuilder($apiKey);
74+
75+
return $mapBuilder;
76+
},
77+
...
78+
```
79+
6580
### Register your service
6681

6782
The next step is to tell UserFrosting to load your service in your [Sprinkle Recipe](/sprinkles/recipe#getservices). To do so, you only need to list all the service providers you want to automatically register inside the `$getServices` property of your sprinkle class :

0 commit comments

Comments
 (0)