# Getting started ## Installing API Platform You can choose your preferred stack between Symfony, Laravel, or bootstrapping the API Platform core library manually. > [!CAUTION] > If you are migrating from an older version of API Platform, make sure you read the [Upgrade Guide](upgrade-guide.md). ### Symfony 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). It comes with the API Platform core library integrated with [the Symfony framework](https://symfony.com), [the schema generator](../schema-generator/index.md), [Doctrine ORM](https://www.doctrine-project.org), [NelmioCorsBundle](https://github.com/nelmio/NelmioCorsBundle) and [test assertions dedicated to APIs](../symfony/testing.md). [MongoDB](mongodb.md) and [Elasticsearch](elasticsearch.md) can also be easily enabled. Basically, it is a Symfony edition packaged with the best tools to develop a REST and GraphQL APIs and sensible default settings. Alternatively, you can use [Composer](https://getcomposer.org/) to install the standalone bundle in an existing Symfony Flex project: ```console composer require api ``` There are no mandatory configuration options although [many settings are available](configuration.md). ### Migrating from FOSRestBundle If you plan to migrate from FOSRestBundle, you might want to read [this guide](../symfony/migrate-from-fosrestbundle.md) to get started with API Platform. ### Laravel API Platform can be installed on any new or existing Laravel project using [API Platform for Laravel](../laravel/index.md). 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)... ### Bootstrapping the Core Library While more complex, the core library [can also be installed in vanilla PHP projects and other frameworks](../core/bootstrap.md). ## Before Reading this Documentation 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). These tutorials cover basic concepts required to understand how API Platform works including how it implements the REST architectural style and what [JSON-LD](https://json-ld.org/) and [Hydra](https://www.hydra-cg.com/) formats are. ## Mapping the Entities ### Symfony with Doctrine

Create an API Resource screencast
Watch the Create an API Resource screencast

API Platform can automatically expose entities mapped as "API resources" through a REST API supporting CRUD operations. To expose your entities, you can use attributes, XML, and YAML configuration files. Here is an example of entities mapped using attributes that will be exposed through a REST API: ```php offers = new ArrayCollection(); // Initialize $offers as a Doctrine collection } public function getId(): ?int { return $this->id; } // Adding both an adder and a remover as well as updating the reverse relation is mandatory // if you want Doctrine to automatically update and persist (thanks to the "cascade" option) the related entity public function addOffer(Offer $offer): void { $offer->product = $this; $this->offers->add($offer); } public function removeOffer(Offer $offer): void { $offer->product = null; $this->offers->removeElement($offer); } // ... } ``` ```php id; } } ``` It is the minimal configuration required to expose `Product` and `Offer` entities as JSON-LD documents through an hypermedia web API. If you are familiar with the Symfony ecosystem, you noticed that entity classes are also mapped with Doctrine ORM attributes and validation constraints from [the Symfony Validator Component](https://symfony.com/doc/current/validation.html). This isn't mandatory. You can use [your preferred persistence](state-providers.md) and [validation](validation.md) systems. However, API Platform has built-in support for those libraries and is able to use them without requiring any specific code or configuration to automatically persist and validate your data. They are a good default option and we encourage you to use them unless you know what you are doing. Thanks to the mapping done previously, API Platform will automatically register the following REST [operations](operations.md) for resources of the product type: ### Product API using Symfony | Method | URL | Description | | ------ | -------------- | ----------------------------------------- | | GET | /products | Retrieve the (paginated) collection | | POST | /products | Create a new product | | GET | /products/{id} | Retrieve a product | | PATCH | /products/{id} | Apply a partial modification to a product | | DELETE | /products/{id} | Delete a product | > [!NOTE] > > `PUT` (replace or create) isn't registered automatically, > but is entirely supported by API Platform and can be added explicitly. > The same operations are available for the offer method (routes will start with the `/offers` pattern). > Route prefixes are built by pluralizing the name of the mapped entity class. > It is also possible to override the naming convention using [operation path namings](operation-path-naming.md). As an alternative to attributes, you can map entity classes using YAML or XML: ```yaml # api/config/api_platform/resources.yaml resources: App\Entity\Product: ~ App\Entity\Offer: shortName: 'Offer' # optional description: 'An offer from my shop' # optional types: ['https://schema.org/Offer'] # optional paginationItemsPerPage: 25 # optional ``` ```xml description="An offer from my shop" > https://schema.org/Offer ``` If you prefer to use YAML or XML files instead of attributes, you must configure API Platform to load the appropriate files: ```yaml # api/config/packages/api_platform.yaml api_platform: mapping: paths: - '%kernel.project_dir%/src/Entity' # default configuration for attributes - '%kernel.project_dir%/config/api_platform' # yaml or xml directory configuration ``` If you want to serialize only a subset of your data, please refer to the [Serialization documentation](serialization.md). **You're done!** You now have a fully featured API exposing your entities. 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`. ### Laravel with Eloquent API Platform introspects the database (column names, types, constraints, types, constraints...) to populate API Platform metadata. Serialization, OpenAPI, and hydra docs are generated from these metadata directly. #### Example First, create a migration class for the `products` table: ```console php artisan make:migration create_products_table ``` Open the generated migration class (`database/migrations/_create_products_table.php`) and add some columns: ```patch public function up(): void { Schema::create('products', function (Blueprint $table) { $table->id(); + $table->string('name'); + $table->decimal('price', 8, 2); + $table->text('description'); + $table->boolean('is_active')->default(true); + $table->date('created_date')->nullable(); $table->timestamps(); }); } ``` Finally, execute the migration: ```console php artisan migrate ``` And after that, just adding the `#[ApiResource]` attribute as follows onto your model: ```patch