Skip to content

Commit 6f225ca

Browse files
soyukadunglas
andauthored
Document laravel provider (#2031)
* add provider * Update laravel/index.md Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> * Update laravel/index.md Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> * review --------- Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
1 parent d34d996 commit 6f225ca

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

laravel/index.md

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,55 @@ So, if you want to access the raw data, you have two alternatives:
165165

166166
For instance, go to `http://127.0.0.1:8000/api/books.jsonld` to retrieve the list of `Book` resources in JSON-LD.
167167

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.
169172
170173
Of course, you can also use your favorite HTTP client to query the API.
171174
We are fond of [Hoppscotch](https://hoppscotch.com), a free and open source API client with good support of API Platform.
172175

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:
174217

175218
```php
176219
<?php
@@ -179,12 +222,14 @@ namespace App\State;
179222

180223
use ApiPlatform\Metadata\Operation;
181224
use ApiPlatform\State\ProviderInterface;
225+
use App\Models\Book as BookModel;
182226

183227
final class BookProvider implements ProviderInterface
184228
{
185229
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
186230
{
187-
return new Book(id: $uriVariables['id']);
231+
$book = BookModel::find($uriVariables['id']);
232+
return new Book(id: $book->id, title: $book->title);
188233
}
189234
}
190235
```
@@ -217,10 +262,16 @@ Apply the provider to your operation:
217262
```php
218263
<?php
219264

220-
#[Get(provider: BookProvider::class)]
265+
namespace App\ApiResource;
266+
267+
use ApiPlatform\Metadata\Get;
268+
use App\State\BookProvider;
269+
270+
#[Get(uriTemplate: '/my_custom_book/{id}', provider: BookProvider::class)]
221271
class Book
222272
{
223273
public string $id;
274+
public string $title;
224275
}
225276
```
226277

0 commit comments

Comments
 (0)