api-platform.com 相关问题

此标记应用于与API平台相关的所有问题,这是一个用于创建API优先Web项目的框架。 API平台包含一组工具,可轻松构建功能齐全的超媒体API(现代格式,文档,授权,过滤,订购,缓存,测试......)以及支撑客户端应用程序。它建立在Symfony组件(PHP)和React(JavaScript)之上。

如何在API平台上保存与实体的嵌套关系

我有两个实体,问题和替代品,其中问题与替代品有一对多关系,我试图通过 POST 到问题发送带有替代品嵌套文档的 JSON...

回答 3 投票 0

在Api平台和Synfony中使用自定义id获取Api资源

当我尝试使用 id 测试端点时出现下一个错误(我有不同类型的 ID(UIID 等)) HTTP 获取 本地主机/用户/0NZqdvET.02b01f4194f7f5bd7edb95dc7fd99a1195707dca 错误 “@

回答 1 投票 0

使用自定义数据提供程序通过 Api 平台返回 graphQL 中的自定义集合

我有一个自定义 DTO 类,我想使用 graphQL 返回该类的集合。使用 REST 就可以正常工作。我正在使用 Api 平台 2.6 和 PHP 8.2 有我的 DTO 课程: 我有一个自定义 DTO 类,我想使用 graphQL 返回该类的集合。使用 REST 就可以正常工作。我正在使用 Api 平台 2.6 和 PHP 8.2 这是我的 DTO 课程 : <?php declare(strict_types=1); namespace App\Dto; use ApiPlatform\Core\Annotation\ApiProperty; use ApiPlatform\Core\Annotation\ApiResource; #[ApiResource( collectionOperations: [ 'get' => [ 'method' => 'GET', 'path' => '/settings', ], ], itemOperations: [ 'get' => [ 'method' => 'GET', 'path' => '/settings/{key}', ], ], routePrefix: '/admin', )] final class SettingDto { #[ApiProperty(identifier: true)] public string $key; public string $value; } 有我的自定义数据提供程序: <?php declare(strict_types=1); namespace App\Api\DataProvider; use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; use App\Dto\SettingDto; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; final readonly class SettingDataProvider implements ItemDataProviderInterface, CollectionDataProviderInterface, RestrictedDataProviderInterface { public function __construct( private ParameterBagInterface $parameterBag, private PropertyAccessorInterface $propertyAccessor, ) {} public function supports(string $resourceClass, string $operationName = null, array $context = []): bool { return is_a($resourceClass, SettingDto::class, true); } /** * @inheritDoc */ public function getCollection(string $resourceClass, string $operationName = null): array { return array_map(static function ($key, $value) { $settingDto = new SettingDto(); $settingDto->key = $key; $settingDto->value = $value; return $settingDto; }, array_keys($this->parameterBag->get('exposed.parameters')), $this->parameterBag->get('exposed.parameters')); } public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?SettingDto { $setting = $this->propertyAccessor->getValue($this->parameterBag->get('exposed.parameters'), "[$id]"); if (null === $setting) { return null; } $settingDto = new SettingDto(); $settingDto->key = $id; $settingDto->value = $setting; return $settingDto; } } 使用 REST 工作正常,但是当我尝试使用 graphQL 时出现此错误: Collection returned by the collection data provider must implement ApiPlatform\\Core\\DataProvider\\PaginatorInterface or ApiPlatform\\Core\\DataProvider\\PartialPaginatorInterface 我也尝试使用像here提到的自定义解析器,但它也不起作用。有什么想法吗? 我终于找到了一个解决方案,我使用了ArrayPaginator,并且我也在我的DTO中做了一些更改,所以现在我恢复了分页(也在REST中): <?php declare(strict_types=1); namespace App\Dto; use ApiPlatform\Core\Annotation\ApiProperty; use ApiPlatform\Core\Annotation\ApiResource; #[ApiResource( collectionOperations: [ 'get' => [ 'method' => 'GET', 'path' => '/settings', ], ], graphql: [ 'item_query', 'collection_query' => [ 'pagination_type' => 'page', ], ], itemOperations: [ 'get' => [ 'method' => 'GET', 'path' => '/settings/{key}', ], ], routePrefix: '/admin', )] final class SettingDto { #[ApiProperty(identifier: true)] public string $key; public string $value; } 还有我的数据提供者: <?php declare(strict_types=1); namespace App\Api\DataProvider; use ApiPlatform\Core\DataProvider\ArrayPaginator; use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface; use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; use App\Dto\SettingDto; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; final readonly class SettingDataProvider implements ItemDataProviderInterface, ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface { public function __construct( private ParameterBagInterface $parameterBag, private PropertyAccessorInterface $propertyAccessor, ) {} public function supports(string $resourceClass, string $operationName = null, array $context = []): bool { return is_a($resourceClass, SettingDto::class, true); } /** * @inheritDoc */ public function getCollection(string $resourceClass, string $operationName = null, array $context = []): ArrayPaginator { $page = $this->propertyAccessor->getValue($context, "[filters][page]") ?? 1; $itemsPerPage = $this->propertyAccessor->getValue($context, "[filters][itemsPerPage]") ?? 10; $firstResult = ($page -1) * $itemsPerPage; $settings = array_map(static function ($key, $value) { $settingDto = new SettingDto(); $settingDto->key = $key; $settingDto->value = $value; return $settingDto; }, array_keys($this->parameterBag->get('exposed.parameters')), $this->parameterBag->get('exposed.parameters')); return new ArrayPaginator($settings, $firstResult, $itemsPerPage); } public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?SettingDto { $setting = $this->propertyAccessor->getValue($this->parameterBag->get('exposed.parameters'), "[$id]"); if (null === $setting) { return null; } $settingDto = new SettingDto(); $settingDto->key = $id; $settingDto->value = $setting; return $settingDto; } }

回答 1 投票 0

具有时间戳属性的实体不起作用

我正在全新安装 API Platform (v3.2.7),并且正在尝试在示例 Greeting 实体上实现 Gedmo 的 Timestampable。我首先尝试使用属性 我正在全新安装 API Platform (v3.2.7),并且我正在尝试在示例 Greeting 实体上实现 Gedmo 的 Timestampable。我首先尝试使用属性 <?php namespace App\Entity; use ApiPlatform\Metadata\ApiResource; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Gedmo\Mapping\Annotation as Gedmo; use Doctrine\DBAL\Types\Types; /** * This is a dummy entity. Remove it! */ #[ApiResource] #[ORM\Entity] class Greeting { /** * The entity ID */ #[ORM\Id] #[ORM\Column(type: 'integer')] #[ORM\GeneratedValue] private ?int $id = null; /** * A nice person */ #[ORM\Column] #[Assert\NotBlank] public string $name = ''; public function getId(): ?int { return $this->id; } #[Gedmo\Timestampable(on: 'create')] #[ORM\Column(type: Types::DATETIME_MUTABLE)] protected $createdAt; #[Gedmo\Timestampable(on: 'update')] #[ORM\Column(type: Types::DATETIME_MUTABLE)] protected $updatedAt; } 但这不起作用,所以我尝试使用 Trait <?php namespace App\Entity; use ApiPlatform\Metadata\ApiResource; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Gedmo\Timestampable\Traits\TimestampableEntity; /** * This is a dummy entity. Remove it! */ #[ApiResource] #[ORM\Entity] class Greeting { use TimestampableEntity; /** * The entity ID */ #[ORM\Id] #[ORM\Column(type: 'integer')] #[ORM\GeneratedValue] private ?int $id = null; /** * A nice person */ #[ORM\Column] #[Assert\NotBlank] public string $name = ''; public function getId(): ?int { return $this->id; } } 在这两种情况下,错误都是: 执行查询时发生异常:SQLSTATE[23000]: 完整性约束违规:1048 列“created_at”不能 空 我过去也尝试过这个对我有用的 # config/services.yaml gedmo.listener.timestampable: class: Gedmo\Timestampable\TimestampableListener tags: - { name: doctrine.event_subscriber, connection: default } calls: - [ setAnnotationReader, [ '@annotation_reader' ] ] 错误是: 服务“gedmo.listener.timestampable”依赖于 不存在的服务“annotation_reader” 如果必须在 symfony 6.3 中弃用 Doctrine Lifecycle Subscribers,我不会这样做 编辑 我在探查器(理论)中看到了这一点,所以我猜这 2 个时间戳被设置为 NULL 而不是正确的值 自从升级到 Symfony 6.4 后,我遇到了完全相同的错误。由于不兼容,我必须删除 sensio/framework-extra-bundle。这可能是一个线索吗?

回答 1 投票 0

Composer 错误,然后尝试使用官方 github 安装 API 平台

所以,我执行了此文档中的所有步骤https://api-platform.com/docs/distribution/: 安装实际版本3.2.2 启动 docker 作曲家 但是当我尝试启动 docker 容器时 docker 组成 -...

回答 1 投票 0

从文档禁用操作

在 API 平台中,我想从文档中隐藏一些操作。所以我做了找不到操作并设置 openapi: false #[API资源( […] 运营: [ 新删除(控制...

回答 1 投票 0

处理器中我们如何获取请求参数?

我有一个自定义处理器,在将数据保存在数据库中之前,我需要根据请求参数(queryString、post body 等)执行一些语句。 在我的示例中,我想获取...

回答 2 投票 0

供应商的 Api 平台模块

我在我的 Api 平台模块中遇到了 Symfony 自动配置存储库的问题。我正在尝试模块化我的 API 和产品模块(https://github.com/ControleOnl...

回答 1 投票 0

ApiPlatform 转换为突变 graphql 上现有实体的最佳方式

我有这个用户实体 #[UniqueEntity(fields: ['email'], message: '电子邮件存在')] #[ORM\实体] #[ORM\Table(名称: '用户')] #[Api平台\Api资源( 运营: [], graphQl操作...

回答 1 投票 0

如何在“/api”文档 api-platform 上添加我的自定义错误

我正在使用 Symfony 4 和 Api-Platform 开发一个应用程序。 我根据这些文档创建了一个自定义错误 xxxException。我在手术后使用过它,效果很好。 现在我想揭露我的错误...

回答 2 投票 0

将 Apiplatform 与外部 OpenID 服务器(Keycloak)连接

我正在寻找将在 Symfony 6.3 上使用 ApiPlatform 3.1 编写的 API 连接到 OpenID 服务器(Keycloak)。不幸的是我找不到任何满足我需求的文档。以下文献...

回答 1 投票 0

ApiPlatform 从 3.1 迁移到 3.2 后,我的 Denormalizer 崩溃了,因为它被发送到 ValidationExceptionNormalizer

受到 Ryan Weaver 关于 SymfonyCast 上的 ApiPlatform 的精彩教程的启发,我创建了一个 Normalizer 和 Denormalizer 来管理组标准化和非标准化。 组反规范化器

回答 1 投票 0

如何在具有平台 API 组的 Trait 中使用 Timestampable

现在我有很多实体: #[ORM\Column(类型:'日期时间')] #[组(['文章类别:输出'])] /** * @Timestampable(on="创建") */ 私人 \DateTime $createdAt; 公共功能...

回答 2 投票 0

如何将 OpenApi 3.0 导入 Api 平台? (先签合同)

我想使用Api平台(symfony 4)来制作API Rest。在开始使用 API Platform 之前,我使用 Api Generator 创建 Openapi 3.0 (swagger)。所以我首先定义合同。 ...

回答 1 投票 0

自定义控制器Api平台的问题

我的控制器不适用于 GET 方法,但适用于 PUT。我不知道为什么。如果有人有答案的话。 我知道在 v2.6 中,操作项和集合项之间存在差异,但在 v3 中

回答 0 投票 0

kernel.view 事件即使已注册也未触发

我正在创建一个用户系统,并希望在通过 POST 方法捕获密码时对密码进行加密。 为此,我使用 Symfony 的 make:subscriber 命令来创建我的函数。 虽然

回答 1 投票 0

为什么每次更新PHP注解(PHP属性)后第一次请求都很慢?

我不知道为什么每次我更改PHP注释(更新验证约束#[Assert/Type],更新#[ApiResource]中的操作...),然后向任何api端点发送新请求时,它t...

回答 1 投票 0

API平台添加或更新

下午好,我正在通过 PLATFORM API 中的 POST 方法添加数据,我可以使此方法像添加或更新数据一样工作吗? 这样当对象的数据已经存在时,它就会简单地增加......

回答 2 投票 0

在 api 平台响应中包含计算出的 SUM 列

我使用 SF 6.3 和最新的 api 平台。 我正在尝试对公司所有职位的所有可用职位进行求和。 $queryBuilder ->leftJoin('公司.jobs', '工作') ->addSelect('S...

回答 1 投票 0

API平台只接受教义类型json_array的数组,如何保存字符串?

我有一个像这样的 Symfony 4.4 学说类: /** * @ApiResource( * 集合操作={ * "GET" = {"security"="is_granted('ROLE_ADMIN')"}, * ...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.