Skip to content

Commit d2f1344

Browse files
Integrates Laravel & bootstraping the core library (#1998)
* Integrates Laravel & bootstraping the core library * Update getting-started.md --------- Co-authored-by: Kévin Dunglas <[email protected]>
1 parent d533dd1 commit d2f1344

File tree

1 file changed

+138
-24
lines changed

1 file changed

+138
-24
lines changed

core/getting-started.md

Lines changed: 138 additions & 24 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](../symfony/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](../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
2950
and what [JSON-LD](https://json-ld.org/) and [Hydra](https://www.hydra-cg.com/) formats are.
3051

3152
## Mapping the Entities
3253

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

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
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,7 +172,7 @@ 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
-------|----------------|--------------------------------
@@ -163,7 +186,6 @@ DELETE | /products/{id} | Delete a product
163186
>
164187
> `PUT` (replace or create) isn't registered automatically,
165188
> but is entirely supported by API Platform and can be added explicitly.
166-
167189
The same operations are available for the offer method (routes will start with the `/offers` pattern).
168190
Route prefixes are built by pluralizing the name of the mapped entity class.
169191
It is also possible to override the naming convention using [operation path namings](operation-path-naming.md).
@@ -182,7 +204,6 @@ resources:
182204
types: ['https://schema.org/Offer'] # optional
183205
paginationItemsPerPage: 25 # optional
184206
```
185-
186207
```xml
187208
<?xml version="1.0" encoding="UTF-8" ?>
188209
<!-- api/config/api_platform/resources.xml -->
@@ -216,14 +237,107 @@ api_platform:
216237
- '%kernel.project_dir%/src/Entity' # default configuration for attributes
217238
- '%kernel.project_dir%/config/api_platform' # yaml or xml directory configuration
218239
```
219-
220240
If you want to serialize only a subset of your data, please refer to the [Serialization documentation](serialization.md).
221-
222241
**You're done!**
223-
224242
You now have a fully featured API exposing your entities.
225243
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`.
226244

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

0 commit comments

Comments
 (0)