问题
I'd like to use YAML instead of annotations in Api-Platform.
Instead of using the Api-Platform distribution, I have added the api-pack into my existing Symfony Flex project (composer req api
).
The documentation says the YAML file should take place in /config/api_platform/resources.yaml
but my entities aren't discovered.
Should I configure something somewhere else?
Thank you, Ben
回答1:
The only thing you need to do is to add the following configuration:
api_platform:
mapping:
paths: ['%kernel.project_dir%/config/api_platform/resources']
I use a subfolder named resources
inside to split the configuration into many files. Here is an example of configuration:
article.yaml
# /config/api_platform/resources/article.yaml
App\Domain\Article:
attributes:
normalization_context:
groups: ['article_read']
collectionOperations: []
itemOperations:
get:
method: 'GET'
put:
method: 'PUT'
user.yaml (with more content in config)
# This file is inside /config/api_platform/resources/user.yaml
App\Domain\User:
attributes:
normalization_context:
groups: ['user_read']
denormalization_context:
api_allow_update: true
groups: ['user_write', 'user_avatar_write']
filters:
- 'App\Application\ApiPlatform\Filters\DeletedFilter'
collectionOperations:
get:
method: 'GET'
access_control: is_granted('VIEW', object)
normalization_context: {'groups': ['user_read_collection']}
post:
method: 'POST'
access_control: is_granted('CREATE', object)
normalization_context:
groups: ['user_post']
itemOperations:
get:
method: 'GET'
access_control: is_granted('VIEW', object)
回答2:
I had the same problem that you, I avoided the problem by using service decorations as described in the documentation.
config/services.yaml
# Customize Swagger documentation
'App\Swagger\SwaggerDecorator':
decorates: 'api_platform.swagger.normalizer.documentation'
arguments: [ '@App\Swagger\SwaggerDecorator.inner' ]
autoconfigure: false
src/Swagger/SwaggerDecorator.php
<?php
namespace App\Swagger;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* @link https://api-platform.com/docs/core/swagger/
*/
final class SwaggerDecorator implements NormalizerInterface
{
private $decorated;
public function __construct(NormalizerInterface $decorated)
{
$this->decorated = $decorated;
}
public function normalize($object, $format = null, array $context = [])
{
$docs = $this->decorated->normalize($object, $format, $context);
$customDefinition = [
'name' => 'id',
'description' => 'ID of user',
'in' => 'path',
'required' => 'true',
];
$docs['paths']['/api/ben/stack_overflow/{id}']['post'] = [
'summary' => 'SO example',
'parameters' => [
$customDefinition,
],
'responses' => [
'200' => [
'description' => 'OK',
],
'400' => [
'description' => 'Error',
],
],
];
}
public function supportsNormalization($data, $format = null)
{
return $this->decorated->supportsNormalization($data, $format);
}
}
The syntax is the same as in Swagger but is written as PHP array instead of JSON.
It will generate the following Swagger documentation :
(I don't know yet how to change the default
title)
来源:https://stackoverflow.com/questions/49788791/api-platform-using-yaml-config-instead-of-annotations-in-sf-4