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
If you plan to migrate from FOSRestBundle, you might want to read [this guide](migrate-from-fosrestbundle.md) to get started with API Platform.
5
+
You can choose your preferred stack between Symfony, Laravel, or bootstrapping the API Platform core library manually.
6
6
7
-
## Installing API Platform
7
+
> [!CAUTION]
8
+
>
9
+
> If you are migrating from an older version of API Platform, make sure you read the [Upgrade Guide](upgrade-guide.md).
10
+
11
+
### Symfony
12
+
13
+
If you are starting a new project, the easiest way to get API Platform up is to install [API Platform for Symfony](../symfony/index.md).
8
14
9
-
If you are starting a new project, the easiest way to get API Platform up is to install the [API Platform Distribution](../symfony/index.md).
10
15
It comes with the API Platform core library integrated with [the Symfony framework](https://symfony.com), [the schema generator](../schema-generator/),
[NelmioCorsBundle](https://github.com/nelmio/NelmioCorsBundle) and [Behat](https://behat.org/).
13
-
[Doctrine MongoDB ODM](https://www.doctrine-project.org/projects/mongodb-odm.html) can also be enabled by following the [MongoDB documentation](mongodb.md).
14
-
Basically, it is a Symfony edition packaged with the best tools to develop a REST API and sensible default settings.
16
+
[Doctrine ORM](https://www.doctrine-project.org),
17
+
[NelmioCorsBundle](https://github.com/nelmio/NelmioCorsBundle) and [test assertions dedicated to APIs](testing.md).
18
+
19
+
[MongoDB](mongodb.md) and [Elasticsearch](elasticsearch.md) can also be easily enabled.
20
+
21
+
Basically, it is a Symfony edition packaged with the best tools to develop a REST and GraphQL APIs and sensible default settings.
15
22
16
23
Alternatively, you can use [Composer](https://getcomposer.org/) to install the standalone bundle in an existing Symfony Flex
17
24
project:
18
25
19
-
`composer require api`
26
+
```console
27
+
composer require API
28
+
```
20
29
21
30
There are no mandatory configuration options although [many settings are available](configuration.md).
22
31
23
-
**Warning**: If you are migrating from an older version of API Platform than 2.7, make sure you read the [Upgrade Guide](upgrade-guide.md).
32
+
### Migrating from FOSRestBundle
33
+
34
+
If you plan to migrate from FOSRestBundle, you might want to read [this guide](migrate-from-fosrestbundle.md) to get started with API Platform.
35
+
36
+
### Laravel
37
+
38
+
API Platform can be installed on any new or existing Laravel project using [API Platform for Laravel](../laravel/index.md).
39
+
40
+
It comes with integrations from the Laravel ecosystem, including [Eloquent](https://laravel.com/docs/eloquent), [Validation](https://laravel.com/docs/validation), [Authorization](https://laravel.com/docs/authorization), [Octane](https://laravel.com/docs/octane), [Pest](https://pestphp.com)...
41
+
42
+
### Bootstrapping the Core Library
43
+
44
+
While more complex, the core library [can also be installed in vanilla PHP projects and other frameworks](../core/bootstrap.md).
24
45
25
46
## Before Reading this Documentation
26
47
27
-
If you haven't read it already, take a look at [the Getting Started guide](../symfony/index.md).
28
-
This tutorial covers basic concepts required to understand how API Platform works including how it implements the REST pattern
48
+
If you haven't read it already, take a look at [the Laravel Getting Started guide](../laravel/index.md) or [the Symfony Getting Started guide](../symfony/index.md).
49
+
These tutorials cover basic concepts required to understand how API Platform works including how it implements the REST architectural style
29
50
and what [JSON-LD](https://json-ld.org/) and [Hydra](https://www.hydra-cg.com/) formats are.
30
51
31
52
## Mapping the Entities
32
53
54
+
### Symfony with Doctrine
55
+
33
56
<palign="center"class="symfonycasts"><ahref="https://symfonycasts.com/screencast/api-platform/api-resource?cid=apip"><imgsrc="/docs/symfony/images/symfonycasts-player.png"alt="Create an API Resource screencast"><br>Watch the Create an API Resource screencast</a></p>
34
57
35
-
API Platform is able to automatically expose entities mapped as "API resources" through a REST API supporting CRUD
58
+
API Platform can automatically expose entities mapped as "API resources" through a REST API supporting CRUD
36
59
operations.
37
-
To expose your entities, you can use attributes, XML and YAML configuration files.
60
+
To expose your entities, you can use attributes, XML, and YAML configuration files.
38
61
39
-
Here is an example of entities mapped using attributes which will be exposed through a REST API:
62
+
Here is an example of entities mapped using attributes that will be exposed through a REST API:
40
63
41
64
```php
42
65
<?php
@@ -149,7 +172,7 @@ them unless you know what you are doing.
149
172
Thanks to the mapping done previously, API Platform will automatically register the following REST [operations](operations.md)
> `PUT` (replace or create) isn't registered automatically,
165
188
> but is entirely supported by API Platform and can be added explicitly.
166
-
167
189
The same operations are available for the offer method (routes will start with the `/offers` pattern).
168
190
Route prefixes are built by pluralizing the name of the mapped entity class.
169
191
It is also possible to override the naming convention using [operation path namings](operation-path-naming.md).
@@ -182,7 +204,6 @@ resources:
182
204
types: ['https://schema.org/Offer'] # optional
183
205
paginationItemsPerPage: 25# optional
184
206
```
185
-
186
207
```xml
187
208
<?xml version="1.0" encoding="UTF-8" ?>
188
209
<!-- api/config/api_platform/resources.xml -->
@@ -216,14 +237,107 @@ api_platform:
216
237
- '%kernel.project_dir%/src/Entity'# default configuration for attributes
217
238
- '%kernel.project_dir%/config/api_platform'# yaml or xml directory configuration
218
239
```
219
-
220
240
If you want to serialize only a subset of your data, please refer to the [Serialization documentation](serialization.md).
221
-
222
241
**You're done!**
223
-
224
242
You now have a fully featured API exposing your entities.
225
243
Run the Symfony app with the [Symfony Local Web Server](https://symfony.com/doc/current/setup/symfony_server.html) (`symfony server:start`) and browse the API entrypoint at `http://localhost:8000/api`.
226
244
227
-
Interact with the API using a REST client (we recommend [Postman](https://www.postman.com/)) or an Hydra-aware application
228
-
(you should give [Hydra Console](https://github.com/lanthaler/HydraConsole) a try). Take
229
-
a look at the usage examples in [the `features` directory](https://github.com/api-platform/core/tree/main/features).
245
+
### Laravel with Eloquent
246
+
247
+
API Platform introspects the database (column names, types, constraints, types, constraints...) to populate API Platform metadata.
248
+
Serialization, OpenAPI, and hydra docs are generated from these metadata directly.
249
+
250
+
#### Example
251
+
252
+
First, create a migration class for the `products` table:
253
+
254
+
```console
255
+
php artisan make:migration create_products_table
256
+
```
257
+
258
+
Open the generated migration class (`database/migrations/<timestamp>_create_products_table.php`) and add some columns:
259
+
260
+
```patch
261
+
public function up(): void
262
+
{
263
+
Schema::create('products', function (Blueprint $table) {
264
+
$table->id();
265
+
266
+
+ $table->string('name');
267
+
+ $table->decimal('price', 8, 2);
268
+
+ $table->text('description');
269
+
+ $table->boolean('is_active')->default(true);
270
+
+ $table->date('created_date')->nullable();
271
+
272
+
$table->timestamps();
273
+
});
274
+
}
275
+
```
276
+
277
+
Finally, execute the migration:
278
+
279
+
```console
280
+
php artisan
281
+
```
282
+
283
+
And after that, just adding the `#[ApiResource]` attribute as follows onto your model:
284
+
285
+
```patch
286
+
<?php
287
+
288
+
namespace App\Models;
289
+
//app/Models/Product.php
290
+
+use ApiPlatform\Metadata\ApiResource;
291
+
use Illuminate\Database\Eloquent\Model;
292
+
293
+
+#[ApiResource]
294
+
class Product extends Model {}
295
+
```
296
+
297
+
While attributes (introduced in PHP 8) are the preferred way to configure your API Platform resources, it’s also possible to use a trait instead.
298
+
```patch
299
+
<?php
300
+
301
+
namespace App\Models;
302
+
//app/Models/Product.php
303
+
+use ApiPlatform\Metadata\IsApiResource;
304
+
use Illuminate\Database\Eloquent\Model;
305
+
306
+
class Product extends Model
307
+
{
308
+
+ use IsApiResource;
309
+
}
310
+
```
311
+
312
+
See the [Using The IsApiResourceTrait Instead of Attributes documentation](../laravel/index.md#using-the-isapiresourcetrait-instead-of-attributes) for examples of advanced configuration in both cases (attributes or static method).
313
+
314
+
**You're done!** 🎉
315
+
316
+
You now have a fully featured API exposing your entities.
317
+
318
+
Run the Laravel app with the Laravel's local development server using [the Artisan Console component](https://laravel.com/docs/artisan) (`php artisan serve`) and browse the API entrypoint at `http://localhost:8000/api`.
319
+
320
+
Using configurations, API Platform generates metadata that will automatically register the following REST [operations](operations.md)
GET | /products | Retrieve the (paginated) collection
328
+
POST | /products | Create a new product
329
+
GET | /products/{id} | Retrieve a product
330
+
PATCH | /products/{id} | Apply a partial modification to a product
331
+
DELETE | /products/{id} | Delete a product
332
+
333
+
In addition, among other things, API Platform under the hood does the following:
334
+
- Generated machine-readable documentations of the API in the [OpenAPI (formerly known as Swagger)](../core/openapi.md) (available at `http://127.0.0.1:8000/api/docs.json`) and [JSON-LD](https://json-ld.org)/[Hydra](https://www.hydra-cg.com) formats using this metadata
335
+
- Generated nice human-readable documentation and a sandbox for the API with [SwaggerUI](https://swagger.io/tools/swagger-ui/) (Redoc is also available out-of-the-box)
336
+
337
+
## Interactions with the API
338
+
339
+
Interact with the API using a REST client (we recommend [Hoppscotch](https://hoppscotch.io/)) or [API Platform Admin](../admin/index.md).
340
+
341
+
## Examples
342
+
343
+
Take a look at [the API Platform demo](https://github.com/api-platform/demo).
0 commit comments