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: laravel/index.md
+55-4Lines changed: 55 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -165,12 +165,55 @@ So, if you want to access the raw data, you have two alternatives:
165
165
166
166
For instance, go to `http://127.0.0.1:8000/api/books.jsonld` to retrieve the list of `Book` resources in JSON-LD.
167
167
168
-
> [!NOTE] Read the next parameter if you want to use JSON:API instead!
168
+
> [!NOTE]
169
+
> Documentation for Eloquent "API resources" encourages using the JSON:API community format.
170
+
> While we recommend preferring JSON-LD when possible, JSON:API is also supported by API Platform,
171
+
> read the [Content Negotiation](#content-negotiation) section to learn how to enable it.
169
172
170
173
Of course, you can also use your favorite HTTP client to query the API.
171
174
We are fond of [Hoppscotch](https://hoppscotch.com), a free and open source API client with good support of API Platform.
172
175
173
-
As recommended by our [design considerations](../core/design.md), you can totally use the data source of your choice using a [provider](../core/state-providers.md):
176
+
177
+
## Using Data Transfer Objects and Hooking Custom Logic
178
+
179
+
While exposing directly the data in the database is convenient for Rapid Application Development, using different classes for the internal data and the public data is a good practice for more complex projects.
180
+
181
+
As explained in our [general design considerations](../core/design.md), API Platform allows us to use the data source of our choice using a [provider](../core/state-providers.md) and Data Transfer Objects (DTOs) are first-class citizens!
182
+
183
+
Let's create our DTO:
184
+
185
+
```php
186
+
<?php
187
+
188
+
namespace App\ApiResource;
189
+
190
+
use ApiPlatform\Metadata\Get;
191
+
192
+
#[Get(uriTemplate: '/my_custom_book/{id}')]
193
+
class Book
194
+
{
195
+
public string $id;
196
+
public string $title;
197
+
}
198
+
```
199
+
200
+
and register our new directory to API Platform:
201
+
202
+
```php
203
+
// config/api-platform.php
204
+
205
+
// ...
206
+
return [
207
+
'resources' => [
208
+
app_path('ApiResource'),
209
+
app_path('Models'),
210
+
],
211
+
212
+
// ...
213
+
];
214
+
```
215
+
216
+
Then we can create the logic to retrieve the state of our `Book` DTO:
174
217
175
218
```php
176
219
<?php
@@ -179,12 +222,14 @@ namespace App\State;
179
222
180
223
use ApiPlatform\Metadata\Operation;
181
224
use ApiPlatform\State\ProviderInterface;
225
+
use App\Models\Book as BookModel;
182
226
183
227
final class BookProvider implements ProviderInterface
184
228
{
185
229
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
186
230
{
187
-
return new Book(id: $uriVariables['id']);
231
+
$book = BookModel::find($uriVariables['id']);
232
+
return new Book(id: $book->id, title: $book->title);
188
233
}
189
234
}
190
235
```
@@ -217,10 +262,16 @@ Apply the provider to your operation:
0 commit comments