我在Symfony 4中使用了api平台。它可以正常工作,但是我想从以下位置更改GET网址:
/booking/{id}
至/booking/{bookingId}
我正在使用自己的DTO对象和自定义数据提供程序(不是Doctrine ORM)。
这是当前有效的@ApiResource
和@ApiProperty
定义:
/**
*
* @ApiResource(
* itemOperations={
* "get"={
* "path"="/booking/{id}",
* },
* "api_bookings_get_item"={
* "swagger_context"={
* "operationId"="getBookingItem",
* "summary"="Retrieves details on a booking",
* "parameters"= {
* {
* "name"="id",
* "description"="Booking ID",
* "default"="15000",
* "in"="path",
* "required"=true,
* "type"="string"
* }
* },
* "responses"={
* "200"={
* "description"="Results retrieved"
* },
* "404"={
* "description"="Booking not found"
* }
* }
* }
* }
* },
* collectionOperations={}
* )
*/
final class Booking
{
/**
* @var string
* @Assert\NotBlank
*
* @ApiProperty(
* identifier=true,
* attributes={
* "swagger_context"={
* "description"="Booking ID",
* "required"=true,
* "type"="string",
* "example"="123456"
* }
* }
* }
*/
public $id;
// other variables
}
但是,如果我将所有引用从'id'
更改为'bookingId'
,它将停止工作,并出现404
错误。这是我对上面的代码所做的更改:
"path"="/booking/{bookingId}"
"name"="bookingId"
public $bookingId;
是否已将api平台硬编码为使用'id'
作为标识符?有什么办法可以改变吗?
答案 0 :(得分:0)
在Api平台中,id参数是硬编码的:
namespace ApiPlatform\Core\DataProvider;
private function extractIdentifiers(array $parameters, array $attributes)
{
if (isset($attributes['item_operation_name'])) {
if (!isset($parameters['id'])) {
throw new InvalidIdentifierException('Parameter "id" not found');
}
但是您可以创建自己的操作并使用所需的参数名称,文档custom operations
中有一个很好的例子