php - ApiPlatform - 创建自定义端点
问题描述
我尝试search
使用 api 平台创建端点。客户端应该通过 SearchInput::class (序列化)发送搜索查询,然后我将使用 elasticSearch 对另一个实体执行搜索,添加我自己的逻辑并使用 SearchOutput::class 作为响应(反序列化)。
该实体与数据库无关,因此我使用 DTO
我遵循了文档https://api-platform.com/docs/core/controllers/但我实际上面临以下错误
The controller for URI \"/api/search\" is not callable: Controller \"PostSearchController\" does neither exist as service nor as class.
消息非常明确,但我无法弄清楚它为什么会发生以及如何解决它。
应用\实体\搜索.php
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\PostSearchController;
use App\Dto\SearchInput;
use App\Dto\SearchOutput;
/**
* @ApiResource(
* input=SearchInput::class,
* output=SearchOutput::class,
* collectionOperations = {
* "search" = {
* "method" = "POST",
* "path" = "/search",
* "controller" = "PostSearchController::class"
* }
* },
* itemOperations = {
*
* }
* )
*/
final class Search
{
public $id;
}
应用\控制器\PostSearchController.php
<?php
namespace App\Controller;
use App\Entity\Search;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class PostSearchController extends AbstractController
{
public function __invoke(Search $data): Search
{
var_dump($data);die;
}
}
symfony 调试:路由器
-------------------------------- -------- -------- ------ -------------------------------------
Name Method Scheme Host Path
-------------------------------- -------- -------- ------ -------------------------------------
_wdt ANY ANY ANY /_wdt/{token}
_profiler_home ANY ANY ANY /_profiler/
_profiler_search ANY ANY ANY /_profiler/search
_profiler_search_bar ANY ANY ANY /_profiler/search_bar
_profiler_phpinfo ANY ANY ANY /_profiler/phpinfo
_profiler_search_results ANY ANY ANY /_profiler/{token}/search/results
_profiler_open_file ANY ANY ANY /_profiler/open
_profiler ANY ANY ANY /_profiler/{token}
_profiler_router ANY ANY ANY /_profiler/{token}/router
_profiler_exception ANY ANY ANY /_profiler/{token}/exception
_profiler_exception_css ANY ANY ANY /_profiler/{token}/exception.css
api_entrypoint ANY ANY ANY /api/{index}.{_format}
api_doc ANY ANY ANY /api/docs.{_format}
api_jsonld_context ANY ANY ANY /api/contexts/{shortName}.{_format}
api_searches_search_collection POST ANY ANY /api/search
api_photos_get_item GET ANY ANY /api/photos/{id}.{_format}
_preview_error ANY ANY ANY /_error/{code}.{_format}
api_login_check ANY ANY ANY /api/login_check
-------------------------------- -------- -------- ------ -------------------------------------
symfony 调试:容器
Select one of the following services to display its information:
[0] App\Controller\PostSearchController
[1] App\Dto\SearchInput
[2] App\Dto\SearchOutput
[3] disallow_search_engine_index_response_listener
[4] api_platform.doctrine.orm.search_filter
[5] annotated_app_entity_photo_api_platform_core_bridge_doctrine_orm_filter_search_filter
[6] ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter
> 0
Information for Service "App\Controller\PostSearchController"
=============================================================
---------------- -------------------------------------
Option Value
---------------- -------------------------------------
Service ID App\Controller\PostSearchController
Class App\Controller\PostSearchController
Tags controller.service_arguments
container.service_subscriber
Calls setContainer
Public yes
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired yes
Autoconfigured yes
---------------- -------------------------------------
解决方案
感谢@yivi 的帮助
解决方法是在 ApiResource 注解中使用 FQCN
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource(
* input="App\Dto\SearchInput",
* output="App\Dto\SearchOutput",
* collectionOperations = {
* "search" = {
* "method" = "post",
* "path" = "/search",
* "controller" = "App\Controller\PostSearchController"
* }
* },
* itemOperations = {
*
* }
* )
*/
final class Search
{
public $id;
}
推荐阅读
- java - 大家好,我想使用 Java 代码在我的文本文件中搜索这个词“(错误:87)”
- html - chrome中左滚动div右侧的未知填充
- python - Python:控制用户输入的变体数量
- wordpress - 像产品目录一样吸引商务
- azure - 唯一自定义属性 Azure ADB2C 租户
- javascript - 无法从回调中更新(分配给变量)的textContent
- php - 如何在数组中分配值 ng-model?
- python - 如何为我的 tensorflow 模型提高此数据管道的性能
- python - KeyError:AIRTABLE_API_KEY
- r - 如何将“...”中包含的参数传递给 R 中的递归调用