Skip to content

Commit 9589dec

Browse files
committed
singleton and scoped attributes
1 parent 6de28dd commit 9589dec

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

container.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,22 @@ $this->app->singletonIf(Transistor::class, function (Application $app) {
178178
});
179179
```
180180

181+
Alternatively, you may mark an interface or class with the `#[Singleton]` attribute to indicate to the container that it should be resolved one time:
182+
183+
```php
184+
<?php
185+
186+
namespace App\Services;
187+
188+
use Illuminate\Container\Attributes\Singleton;
189+
190+
#[Singleton]
191+
class Transistor
192+
{
193+
// ...
194+
}
195+
```
196+
181197
<a name="binding-scoped"></a>
182198
#### Binding Scoped Singletons
183199

@@ -201,6 +217,22 @@ $this->app->scopedIf(Transistor::class, function (Application $app) {
201217
});
202218
```
203219

220+
Alternatively, you may mark an interface or class with the `#[Scoped]` attribute to indicate to the container that it should be resolved one time within a given Laravel request / job lifecycle:
221+
222+
```php
223+
<?php
224+
225+
namespace App\Services;
226+
227+
use Illuminate\Container\Attributes\Scoped;
228+
229+
#[Scoped]
230+
class Transistor
231+
{
232+
// ...
233+
}
234+
```
235+
204236
<a name="binding-instances"></a>
205237
#### Binding Instances
206238

0 commit comments

Comments
 (0)