Skip to content

Commit 082a1d1

Browse files
committed
Merge branch '4.0'
2 parents a05b89a + 93b57c3 commit 082a1d1

34 files changed

+292
-156
lines changed

admin/getting-started.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Installation
44

5-
If you use the [API Platform Distribution](../distribution/), API Platform Admin is already installed, you can skip this installation guide.
5+
If you use the [API Platform Symfony variant](../symfony/), API Platform Admin is already installed, you can skip this installation guide.
66

77
Otherwise, follow this guide.
88

@@ -37,7 +37,7 @@ export default () => (
3737
Be sure to make your API send proper [CORS HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) to allow
3838
the admin's domain to access it.
3939

40-
To do so, if you use the API Platform Distribution, update the value of the `CORS_ALLOW_ORIGIN` parameter in `api/.env` (it will be set to `^https?://localhost:?[0-9]*$`
40+
To do so, if you use the API Platform Symfony variant, update the value of the `CORS_ALLOW_ORIGIN` parameter in `api/.env` (it will be set to `^https?://localhost:?[0-9]*$`
4141
by default).
4242

4343
If you use a custom installation of Symfony and [API Platform Core](../core/), you will need to adjust the [NelmioCorsBundle configuration](https://github.com/nelmio/NelmioCorsBundle#configuration) to expose the `Link` HTTP header and to send proper CORS headers on the route under which the API will be served (`/api` by default).

admin/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ library to expose a nice, responsive, management interface (Create-Retrieve-Upda
1717

1818
You can **customize everything** by using provided React Admin and [MUI](https://mui.com/) components, or by writing your custom [React](https://reactjs.org/) components.
1919

20-
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/react-admin?cid=apip"><img src="../distribution/images/symfonycasts-player.png" alt="React Admin Screencast"><br>Watch the React Admin screencast</a></p>
20+
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/react-admin?cid=apip"><img src="../symfony/images/symfonycasts-player.png" alt="React Admin Screencast"><br>Watch the React Admin screencast</a></p>
2121

2222
## Features
2323

core/content-negotiation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Using the raw JSON or raw XML formats is discouraged, prefer using JSON-LD inste
99

1010
API Platform also supports [JSON Merge Patch (RFC 7396)](https://tools.ietf.org/html/rfc7396) the JSON:API [`PATCH`](https://jsonapi.org/format/#crud-updating) formats, as well as [Problem Details (RFC 7807)](https://tools.ietf.org/html/rfc7807), [Hydra](https://www.hydra-cg.com/spec/latest/core/#description-of-http-status-codes-and-errors) and [JSON:API](https://jsonapi.org/format/#errors) error formats.
1111

12-
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/formats?cid=apip"><img src="/docs/distribution/images/symfonycasts-player.png" alt="Formats screencast"><br>Watch the Formats screencast</a></p>
12+
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/formats?cid=apip"><img src="../symfony/images/symfonycasts-player.png" alt="Formats screencast"><br>Watch the Formats screencast</a></p>
1313

1414
API Platform will automatically detect the best resolving format depending on:
1515

core/design.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ API framework. However, the "design-first" methodology is strongly recommended:
55
API endpoints.
66

77
To do so, you have to write a plain old PHP object (POPO) representing the input and output of your endpoint. This is the class
8-
that is [marked with the `#[ApiResource]` attribute](../distribution/index.md).
8+
that is [marked with the `#[ApiResource]` attribute](../symfony/index.md).
99
This class **doesn't have** to be mapped with Doctrine ORM, or any other persistence system. It must be simple (it's usually
1010
just a data structure with no or minimal behaviors) and will be automatically converted to [Hydra](extending-jsonld-context.md),
1111
[OpenAPI](openapi.md) and [GraphQL](graphql.md) documentations or schemas by API Platform (there is a 1-1 mapping

core/extending-jsonld-context.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## JSON-LD
44

5-
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/json-ld?cid=apip"><img src="/docs/distribution/images/symfonycasts-player.png" alt="JSON-LD screencast"><br>Watch the JSON-LD screencast</a></p>
5+
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/json-ld?cid=apip"><img src="../symfony/images/symfonycasts-player.png" alt="JSON-LD screencast"><br>Watch the JSON-LD screencast</a></p>
66

77
API Platform provides the possibility to extend the JSON-LD context of properties. This allows you to describe JSON-LD-typed
88
values, inverse properties using the `@reverse` keyword and you can even overwrite the `@id` property this way. Everything you define
@@ -63,7 +63,7 @@ Note that you do not have to provide the `@id` attribute. If you do not provide
6363

6464
## Hydra
6565

66-
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/hydra?cid=apip"><img src="/docs/distribution/images/symfonycasts-player.png" alt="Hydra screencast"><br>Watch the Hydra screencast</a></p>
66+
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/hydra?cid=apip"><img src="../symfony/images/symfonycasts-player.png" alt="Hydra screencast"><br>Watch the Hydra screencast</a></p>
6767

6868
It's also possible to replace the Hydra context used by the documentation generator:
6969

core/extending.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ For instance, if you want to send a mail after a resource has been persisted, bu
3535

3636
To replace existing API Platform services with your decorators, [check out how to decorate services](https://symfony.com/doc/current/service_container/service_decoration.html).
3737

38-
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform-security/service-decoration?cid=apip"><img src="/docs/distribution/images/symfonycasts-player.png" alt="Service Decoration screencast"><br>Watch the Service Decoration screencast</a></p>
38+
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform-security/service-decoration?cid=apip"><img src="../symfony/images/symfonycasts-player.png" alt="Service Decoration screencast"><br>Watch the Service Decoration screencast</a></p>

core/filters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ By default, all filters are disabled. They must be enabled explicitly.
1212
When a filter is enabled, it automatically appears in the [OpenAPI](openapi.md) and [GraphQL](graphql.md) documentations.
1313
It is also automatically documented as a `hydra:search` property for JSON-LD responses.
1414

15-
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/filters?cid=apip"><img src="/docs/distribution/images/symfonycasts-player.png" alt="Filtering and Searching screencast"><br>Watch the Filtering & Searching screencast</a></p>
15+
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/filters?cid=apip"><img src="../symfony/images/symfonycasts-player.png" alt="Filtering and Searching screencast"><br>Watch the Filtering & Searching screencast</a></p>
1616

1717
## Parameters
1818

core/getting-started.md

Lines changed: 143 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,65 @@
11
# Getting started
22

3-
## Migrating from FOSRestBundle
3+
## Installing API Platform
44

5-
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.
66

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).
814

9-
If you are starting a new project, the easiest way to get API Platform up is to install the [API Platform Distribution](../distribution/index.md).
1015
It comes with the API Platform core library integrated with [the Symfony framework](https://symfony.com), [the schema generator](../schema-generator/),
11-
[Doctrine ORM](https://www.doctrine-project.org), [Elasticsearch-PHP](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html),
12-
[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.
1522

1623
Alternatively, you can use [Composer](https://getcomposer.org/) to install the standalone bundle in an existing Symfony Flex
1724
project:
1825

19-
`composer require api`
26+
```console
27+
composer require API
28+
```
2029

2130
There are no mandatory configuration options although [many settings are available](configuration.md).
2231

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).
2445

2546
## Before Reading this Documentation
2647

27-
If you haven't read it already, take a look at [the Getting Started guide](../distribution/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
2950
and what [JSON-LD](https://json-ld.org/) and [Hydra](https://www.hydra-cg.com/) formats are.
3051

3152
## Mapping the Entities
3253

33-
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/api-resource?cid=apip"><img src="/docs/distribution/images/symfonycasts-player.png" alt="Create an API Resource screencast"><br>Watch the Create an API Resource screencast</a></p>
54+
### Symfony with Doctrine
3455

35-
API Platform is able to automatically expose entities mapped as "API resources" through a REST API supporting CRUD
56+
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/api-resource?cid=apip"><img src="../symfony/images/symfonycasts-player.png" alt="Create an API Resource screencast"><br>Watch the Create an API Resource screencast</a></p>
57+
58+
API Platform can automatically expose entities mapped as "API resources" through a REST API supporting CRUD
3659
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.
3861

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

4164
```php
4265
<?php
@@ -149,17 +172,20 @@ them unless you know what you are doing.
149172
Thanks to the mapping done previously, API Platform will automatically register the following REST [operations](operations.md)
150173
for resources of the product type:
151174

152-
### Product
175+
### Product API using Symfony
153176

154177
Method | URL | Description
155178
-------|----------------|--------------------------------
156179
GET | /products | Retrieve the (paginated) collection
157180
POST | /products | Create a new product
158181
GET | /products/{id} | Retrieve a product
159-
PUT | /products/{id} | Update a product
160182
PATCH | /products/{id} | Apply a partial modification to a product
161183
DELETE | /products/{id} | Delete a product
162184

185+
> [!NOTE]
186+
>
187+
> `PUT` (replace or create) isn't registered automatically,
188+
> but is entirely supported by API Platform and can be added explicitly.
163189
The same operations are available for the offer method (routes will start with the `/offers` pattern).
164190
Route prefixes are built by pluralizing the name of the mapped entity class.
165191
It is also possible to override the naming convention using [operation path namings](operation-path-naming.md).
@@ -178,7 +204,6 @@ resources:
178204
types: ['https://schema.org/Offer'] # optional
179205
paginationItemsPerPage: 25 # optional
180206
```
181-
182207
```xml
183208
<?xml version="1.0" encoding="UTF-8" ?>
184209
<!-- api/config/api_platform/resources.xml -->
@@ -212,14 +237,107 @@ api_platform:
212237
- '%kernel.project_dir%/src/Entity' # default configuration for attributes
213238
- '%kernel.project_dir%/config/api_platform' # yaml or xml directory configuration
214239
```
215-
216240
If you want to serialize only a subset of your data, please refer to the [Serialization documentation](serialization.md).
217-
218241
**You're done!**
219-
220242
You now have a fully featured API exposing your entities.
221243
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`.
222244

223-
Interact with the API using a REST client (we recommend [Postman](https://www.postman.com/)) or an Hydra-aware application
224-
(you should give [Hydra Console](https://github.com/lanthaler/HydraConsole) a try). Take
225-
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)
321+
for resources of the product type:
322+
323+
### Product API using Laravel
324+
325+
Method | URL | Description
326+
-------|----------------|--------------------------------
327+
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).

core/graphql.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ docker compose exec php sh -c '
2121

2222
You can now use GraphQL at the endpoint: `https://localhost:8443/graphql`.
2323

24-
*Note:* If you used [Symfony Flex to install API Platform](../distribution/index.md#using-symfony-flex-and-composer-advanced-users), URLs will be prefixed with `/api` by default. For example, the GraphQL endpoint will be: `https://localhost:8443/api/graphql`.
24+
*Note:* If you used [Symfony Flex to install API Platform](../symfony/index.md#using-symfony-flex-and-composer-advanced-users), URLs will be prefixed with `/api` by default. For example, the GraphQL endpoint will be: `https://localhost:8443/api/graphql`.
2525

2626
## Changing Location of the GraphQL Endpoint
2727

0 commit comments

Comments
 (0)