@@ -203,10 +203,44 @@ Thanks to the mapping done previously, API Platform will automatically register
203203> of the mapped entity class. It is also possible to override the naming convention using
204204> [ operation path namings] ( operation-path-naming.md ) .
205205
206- As an alternative to attributes, you can map entity classes using YAML or XML:
206+ As an alternative to attributes, you can map entity classes using PHP, YAML or XML files :
207207
208208<code-selector >
209209
210+ ``` php
211+ <?php
212+ // config/api_platform/resources/product.php
213+
214+ use ApiPlatform\Metadata\ApiResource;
215+ use App\Entity\Product;
216+
217+ return (new ApiResource())->withClass(Product::class);
218+ ```
219+
220+ ``` php
221+ <?php
222+ // config/api_platform/resources/offer.php
223+
224+ use ApiPlatform\Metadata\ApiResource;
225+ use ApiPlatform\Metadata\Get;
226+ use ApiPlatform\Metadata\GetCollection;
227+ use ApiPlatform\Metadata\Operations;
228+ use ApiPlatform\Metadata\Post;
229+ use App\Entity\Offer;
230+
231+ return (new ApiResource())
232+ ->withClass(Offer::class)
233+ ->withShortName('Offer')
234+ ->withDescription('An offer from my shop')
235+ ->withTypes(['https://schema.org/Offer'])
236+ ->withPaginationItemsPerPage(25)
237+ ->withOperations(new Operations([
238+ new Get(),
239+ new GetCollection(),
240+ new Post(),
241+ ]));
242+ ```
243+
210244``` yaml
211245# api/config/api_platform/resources.yaml
212246resources :
@@ -241,8 +275,8 @@ resources:
241275
242276</code-selector >
243277
244- If you prefer to use YAML or XML files instead of attributes, you must configure API Platform to
245- load the appropriate files:
278+ If you prefer to use YAML, XML or PHP files instead of attributes, you must configure API Platform
279+ to load the appropriate files:
246280
247281``` yaml
248282# api/config/packages/api_platform.yaml
@@ -251,8 +285,17 @@ api_platform:
251285 paths :
252286 - " %kernel.project_dir%/src/Entity" # default configuration for attributes
253287 - " %kernel.project_dir%/config/api_platform" # yaml or xml directory configuration
288+ imports :
289+ - " %kernel.project_dir%/config/api_platform/resources" # php directory configuration
254290` ` `
255291
292+ > [!NOTE]
293+ >
294+ > PHP resource files must be placed in a directory listed under ` mapping.imports` (not
295+ > `mapping.paths`). Each file must return an `ApiResource` instance. Since these files are not
296+ > autoloaded, they won't conflict with your entity classes. This approach provides the same fluent
297+ > API as PHP attributes.
298+
256299If you want to serialize only a subset of your data, please refer to the
257300[Serialization documentation](serialization.md). **You're done!** You now have a fully featured API
258301exposing your entities. Run the Symfony app with the
0 commit comments