api-platform.com 相关问题

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

具有 LexikJWTAuthenticationBundle 和自定义登录响应的 Api 平台

我使用 symfony 7 和 api 平台以及 LexikJWTAuthenticationBundle 我有 onAuthenticationSuccessResponse 我使用 symfony 7 和 api 平台以及 LexikJWTAuthenticationBundle 我有 onAuthenticationSuccessResponse <?php namespace App\EventListener; use App\DTO\User\LoginResponse; use App\Entity\User; use App\Exception\InvalidUserStatusException; use App\Util\ApiMessageUtil; use App\Util\UserValidator; use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent; use Symfony\Component\EventDispatcher\Attribute\AsEventListener; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Serializer\SerializerInterface; #[AsEventListener(event: 'lexik_jwt_authentication.on_authentication_success', method: 'onAuthenticationSuccessResponse')] readonly class AuthenticationSuccessListener { public function __construct(private SerializerInterface $serializer) { } public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event): void { $data = $event->getData(); /** @var User $user */ $user = $event->getUser(); if (!$user instanceof UserInterface) { return; } try { UserValidator::validateIfUserIsInactive($user); } catch (InvalidUserStatusException $exception) { throw new HttpException(Response::HTTP_UNAUTHORIZED); } $data['user'] = json_decode($this->serializer->serialize($user, 'json', ['groups' => 'login']), true); $event->setData($data); } } 它工作正常,但在示例响应中我仍然只看到“令牌” 我想调整示例响应。我创建了 DTO 类 LoginResponse 但如何在 openapi swagger 登录方法中使用它? 有什么想法吗? 在 services.json 中进行设置而不是使用注释时,它对我有用。来自官方文档: # config/services.yaml services: acme_api.event.authentication_success_listener: class: App\EventListener\AuthenticationSuccessListener tags: - { name: kernel.event_listener, event: lexik_jwt_authentication.on_authentication_success, method: onAuthenticationSuccessResponse } 您可能需要专门设置您的SerializerInterface参数,像这里(我下面的代码未经测试): # config/services.yaml services: acme_api.event.authentication_success_listener: class: App\EventListener\AuthenticationSuccessListener arguments: [ '@serializer_interface' ] tags: - { name: kernel.event_listener, event: lexik_jwt_authentication.on_authentication_success, method: onAuthenticationSuccessResponse }

回答 1 投票 0

使用symfony在apiplatform上调用两次关系问题

我有两个实体:标签、帖子。 关于多对多 我在 GET 中有一个 API 路由:/api/tags/{id} 我想通过标签返回与该标签关联的帖子。 但我也想归还标签

回答 2 投票 0

使用API平台创建具有多对多关系的实体

最近我开始了一个基于API Platform的项目。我像设计其他 Symfony 项目一样设计了我的实体。创建/更新没有关系的正常实体非常好...

回答 5 投票 0

更新API Platform 3.0相关实体

我正在尝试通过主实体更新相关实体。例如,我有产品和优惠。我向 /products 发了一个帖子 { "name": "样品产品", “优惠”:...

回答 2 投票 0

具有 TimestampableEntity 特征的实体在 PUT 操作中失败

我正在全新安装 API Platform (v3.2.7),并且使用 Gedmo\Timestampable\Traits\TimestampableEntity 这是我的实体(问候语示例) 我正在全新安装 API Platform (v3.2.7),并且正在使用 Gedmo\Timestampable\Traits\TimestampableEntity 这是我的实体(问候语示例) <?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; #[ApiResource] #[ORM\Entity] class Greeting { use TimestampableEntity; #[ORM\Id] #[ORM\Column(type: "integer")] #[ORM\GeneratedValue] private ?int $id = null; #[ORM\Column] #[Assert\NotBlank] public string $name = ""; public function getId(): ?int { return $this->id; } } 和我的 config/services.yaml gedmo.listener.timestampable: class: Gedmo\Timestampable\TimestampableListener tags: - { name: doctrine.event_listener, event: 'prePersist' } - { name: doctrine.event_listener, event: 'onFlush' } - { name: doctrine.event_listener, event: 'loadClassMetadata' } 它在 POST 操作上工作正常,但在执行 PUT 时失败。我收到此错误 执行查询时发生异常:SQLSTATE[23000]: 完整性约束违规:1048 列“created_at”不能 空 我使用的版本是:symfony 6.4.1,doctrine 2.12,gedmo 3.14 我最终做的是使用 PATCH 而不是 PUT。所以我可以编辑部分实体,并且特征仍然更新 updated_at 字段

回答 1 投票 0

如何使用 API Platform 和 Gedmo Translatable 查询可翻译属性

我正在使用 Symfony 6 + API 平台 + Gedmo Translatable。 更新/发布项目工作正常并正确存储翻译。 另外,以正确的语言获得集合是......

回答 1 投票 0

如何配置ApiResource注解

我已将此注释添加到我的一个实体中,正如我在文档的最新版本中看到的那样,使用注释而不是属性(https://api-platform.com/docs/v2.5/core/serialization/) : *@

回答 1 投票 0

作为参数传递时,教义实体未更新

我有一个 Doctrine 实体的实例,它作为参数传递给 Symfony 服务的方法。 我在方法之外进行了所有数据操作,但由于某种原因,这些更改没有反映出来......

回答 1 投票 0

API平台,如何使用Security作为GET Collections的过滤器?

我正在以这种方式保护我的实体的安全: /** * @ApiResource( * 集合操作={ *“帖子”={ *“security_post_denormalize”=“is_granted('

回答 2 投票 0

Angular 16:获取 APLI 平台 Hydra 成员,请求中的 api

[在此输入图像描述](https://i.stack.imgur.com/VL8Fx.png) 我想创建一个表,其中呈现以下数据。 Id、sessionStart 和 SessionEnd 已提取到表中并且

回答 1 投票 0

如何在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

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