在Api平台中需要过滤器 [英] Filter required in Api Platform

查看:36
本文介绍了在Api平台中需要过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用API​​平台,并且在之后定义了一个自定义过滤器https://api-platform.com/docs/core/filters/#creating-custom-filters

Im using API Platform and I have defined a custom filter following https://api-platform.com/docs/core/filters/#creating-custom-filters

它可以正常工作,但是每次应用程序对特定实体(设置了过滤器)执行GET HTTP请求时,我都需要使用该过滤器.

It works ok, but I need to that filter is required everytime that application do a GET HTTP Request of particular entity (where the filter is set).

我已经检查了以下代码:

I have check this code:

// This function is only used to hook in documentation generators (supported by Swagger and Hydra)
public function getDescription(string $resourceClass): array
{
    if (!$this->properties) {
        return [];
    }

    $description = [];
    foreach ($this->properties as $property => $strategy) {
        $description["similar_$property"] = [
            'property' => $property,
            'type' => 'string',
            'required' => false,
            'swagger' => [
                'description' => 'Filter using a similar postgres function.',
                'name' => $property,
                'type' => 'string',
            ],
        ];
    }

    return $description;
}

尽管getDescription具有必填字段,但仅适用于api文档,不适用于HTTP请求

Although getDescription has a required field, it only works for api documentation and not for HTTP Request

推荐答案

您可以通过事件系统强制执行过滤器.

You can enforce a filter through the event system.

/**
 * @ApiResource
 */
class Resource {
    ...
}

<?php

namespace App\EventSubscriber;

use ApiPlatform\Core\EventListener\EventPriorities;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\KernelEvents;

class ResourceReadSubscriber implements EventSubscriberInterface
{
    /**
     * @return array The event names to listen to
     */
    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::CONTROLLER => [
                ['hasFilter', EventPriorities::PRE_READ],
            ],
        ];
    }

    public function hasFilter(ControllerEvent $event)
    {
        // first check if this affects the requested resource
        $resource = $event->getRequest()->attributes->get('_api_resource_class');

        if (Resource::class !== $resource) {
            return;
        }

        // second check if this is the get_collection controller
        $controller = $event->getRequest()->attributes->get('_controller');

        if ('api_platform.action.get_collection' !== $controller) {
            return;
        }

        // third validate the required filter is set
        // we expect a filter via GET parameter 'filter-query-parameter'
        if (!$event->getRequest()->query->has('filter-query-parameter')) {
            throw new BadRequestHttpException('Filter is required');
        }
    }
}

这篇关于在Api平台中需要过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆