You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/state-providers.md
+19-24Lines changed: 19 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -58,19 +58,23 @@ use ApiPlatform\State\ProviderInterface;
58
58
*/
59
59
final class BlogPostProvider implements ProviderInterface
60
60
{
61
-
private const DATA = [
62
-
'ab' => new BlogPost('ab'),
63
-
'cd' => new BlogPost('cd'),
64
-
];
61
+
private array $data;
62
+
63
+
public function __construct() {
64
+
$this->data = [
65
+
'ab' => new BlogPost('ab'),
66
+
'cd' => new BlogPost('cd'),
67
+
];
68
+
}
65
69
66
70
public function provide(Operation $operation, array $uriVariables = [], array $context = []): BlogPost|null
67
71
{
68
-
return self::DATA[$uriVariables['id']] ?? null;
72
+
return $this->data[$uriVariables['id']] ?? null;
69
73
}
70
74
}
71
75
```
72
76
73
-
For the example, we store the list of our blog posts in an associative array (the `BlogPostProvider::DATA` constant).
77
+
For the example, we store the list of our blog posts in an associative array `$data`.
74
78
75
79
As this operation expects a `BlogPost`, the `provide` methods return the instance of the `BlogPost` corresponding to the ID passed in the URL. If the ID doesn't exist in the associative array, `provide()` returns `null`. API Platform will automatically generate a 404 response if the provider returns `null`.
76
80
@@ -110,18 +114,15 @@ use ApiPlatform\Metadata\CollectionOperationInterface;
110
114
*/
111
115
final class BlogPostProvider implements ProviderInterface
112
116
{
113
-
private const DATA = [
114
-
'ab' => new BlogPost('ab'),
115
-
'cd' => new BlogPost('cd'),
116
-
];
117
+
private array $data;
117
118
118
119
public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable|BlogPost|null
119
120
{
120
121
if ($operation instanceof CollectionOperationInterface) {
121
-
return self::DATA;
122
+
return $this->data;
122
123
}
123
124
124
-
return self::DATA[$uriVariables['id']] ?? null;
125
+
return $this->data[$uriVariables['id']] ?? null;
125
126
}
126
127
}
127
128
```
@@ -169,19 +170,16 @@ use ApiPlatform\State\ProviderInterface;
169
170
*/
170
171
final class BlogPostProvider implements ProviderInterface
171
172
{
172
-
private const DATA = [
173
-
'ab' => new BlogPost('ab'),
174
-
'cd' => new BlogPost('cd'),
175
-
];
173
+
private array $data;
176
174
177
175
public function provide(Operation $operation, array $uriVariables = [], array $context = []): BlogPost|null
178
176
{
179
-
return self::DATA[$uriVariables['id']] ?? null;
177
+
return $this->data[$uriVariables['id']] ?? null;
180
178
}
181
179
}
182
180
```
183
181
184
-
For the example, we store the list of our blog posts in an associative array (the `BlogPostProvider::DATA` constant).
182
+
For the example, we store the list of our blog posts in an associative array `$data`.
185
183
186
184
As this operation expects a `BlogPost`, the `provide` methods return the instance of the `BlogPost` corresponding to the ID passed in the URL. If the ID doesn't exist in the associative array, `provide()` returns `null`. API Platform will automatically generate a 404 response if the provider returns `null`.
187
185
@@ -221,18 +219,15 @@ use ApiPlatform\Metadata\CollectionOperationInterface;
221
219
*/
222
220
final class BlogPostProvider implements ProviderInterface
223
221
{
224
-
private const DATA = [
225
-
'ab' => new BlogPost('ab'),
226
-
'cd' => new BlogPost('cd'),
227
-
];
222
+
private array $data;
228
223
229
224
public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable|BlogPost|null
230
225
{
231
226
if ($operation instanceof CollectionOperationInterface) {
0 commit comments