ApiPlatform - 创建自定义端点
ApiPlatform - create custom endpoint
我尝试使用 api 平台创建一个 search
端点。
客户端应通过 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.
消息非常明确,但我不知道为什么会发生以及如何解决它。
App\Entity\Search.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;
}
App\Controller\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 debug:router
-------------------------------- -------- -------- ------ -------------------------------------
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 debug:container
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;
}
我尝试使用 api 平台创建一个 search
端点。
客户端应通过 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.
消息非常明确,但我不知道为什么会发生以及如何解决它。
App\Entity\Search.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;
}
App\Controller\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 debug:router
-------------------------------- -------- -------- ------ -------------------------------------
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 debug:container
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;
}