有人可以帮忙吗API平台用户界面。在文档中,我无法找到定义服务器的方法:
我希望服务器端点输出显示是否开启本地主机, dev, or prod.
有人可以帮忙解决这个问题吗:
我在 Symfony 中使用它。
我不确定它是否与路由文件夹中的 api_platform.yaml 文件中的配置有关:
api_platform:
resource: .
type: api_platform
prefix: /
我想配置服务器路径,例如:{domain/API_ROUTE}
where API_ROUTE
定义为.env
多变的。
你需要装饰OpenApiFactory
。对于如何以及为什么,在a中详细解释了SymfonyCast https://symfonycasts.com/screencast/api-platform-security/open-api-decoration.
对于一个工作示例:在您的项目中创建一个文件/src/ApiPlatform/OpenApiFactoryDecorator.php
包含以下内容:
<?php
namespace App\ApiPlatform;
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\OpenApi\Model\Server;
use ApiPlatform\OpenApi\OpenApi;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
#[AsDecorator('api_platform.openapi.factory')]
class OpenApiFactoryDecorator implements OpenApiFactoryInterface
{
public function __construct(private OpenApiFactoryInterface $decorated) {}
public function __invoke(array $context = []): OpenApi
{
$openApi = $this->decorated->__invoke($context);
// Create an array with your servers.
$servers = [
new Server("https://dev.example.com/", "Development"),
new Server("https://prod.example.com/", "Production")
];
return $openApi->withServers($servers);
}
}
上面的示例代码应该可以在具有 API Platform v3.1.* 的 Symfony v6.3.* 中运行,并在文档中提供配置服务器所需的内容。
MIND:
每当您禁用文档(作为示例)时prod
环境中,您很可能会遇到上述代码的问题。每当您禁用文档时可能会出现的错误:The service "App\ApiPlatform\OpenApiFactoryDecorator" has a dependency on a non-existent service "api_platform.openapi.factory".
。为了解决这个问题,您应该使用以下方法忽略无效引用$onInvalid
参数如下:
#[AsDecorator(
decorates: 'api_platform.openapi.factory',
onInvalid: ContainerInterface::IGNORE_ON_INVALID_REFERENCE, // This line is important to ignore errors whenever you disable the documentation in a specific environment.
)]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)