> For the complete documentation index, see [llms.txt](https://docs.ecotone.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ecotone.tech/modelling/command-handling/external-command-handlers/query-handling.md).

# Query Handling

Query CQRS PHP

Be sure to read [CQRS Introduction](/modelling/command-handling.md) before diving in this chapter.

## Handling Queries

`External Query Handlers` are Services available in your dependency container, which are defined to handle `Queries`.

```php
class TicketService
{
    #[QueryHandler] 
    public function getTicket(GetTicketById $query) : array
    {
        //return ticket
    }
}
```

Queries are Plain Old PHP Objects:

```php
class readonly GetTicketById
{
    public function __construct(
        public string $ticketId
    ) {}
}
```

To send an Query we will be using `send` method on `QueryBus`.\
Query will be delivered to corresponding Query Handler.

{% tabs %}
{% tab title="Symfony / Laravel" %}

```php
class TicketController
{
   // Query Bus will be auto registered in Depedency Container.
   public function __construct(private QueryBus $queryBus) {}
   
   public function createTicketAction(Request $request) : Response
   {
      $result = $this->queryBus->send(
         new GetTicketById(
            $request->get("ticketId")            
         )
      );
      
      return new Response(\json_encode($result));
   }
}
```

{% endtab %}

{% tab title="Lite" %}

```php
$ticket = $messagingSystem->getQueryBus()->send(
  new GetTicketById(
    $ticketId            
  )
);
```

{% endtab %}
{% endtabs %}

## Sending with Routing

Just like with Commands, we may use routing in order to execute queries:

```php
class TicketService
{
    #[QueryHandler("ticket.getById")] 
    public function getTicket(string $ticketId) : array
    {
        //return ticket
    }
}
```

To send an Query we will be using `sendWithRouting` method on `QueryBus`.\
Query will be delivered to corresponding Query Handler.

{% tabs %}
{% tab title="Symfony / Laravel" %}

```php
class TicketController
{
   public function __construct(private QueryBus $queryBus) {}
   
   public function createTicketAction(Request $request) : Response
   {
      $result = $this->queryBus->sendWithRouting(
         "ticket.getById",
         $request->get("ticketId")            
      );
      
      return new Response(\json_encode($result));
   }
}
```

{% endtab %}

{% tab title="Lite" %}

```php
$ticket = $messagingSystem->getQueryBus()->sendWithRouting(
   "ticket.getById",
   $ticketId            
);
```

{% endtab %}
{% endtabs %}

## Converting result from Query Handler

If you have registered [Converter](/messaging/conversion.md) for specific Media Type, then you can tell `Ecotone` to convert result of your `Query Bus` to specific format.\
In order to do this, we need to make use of `Metadata`and `replyContentType` header.

{% tabs %}
{% tab title="Symfony / Laravel" %}

```php
class TicketController
{
   public function __construct(private QueryBus $queryBus) {}
   
   public function createTicketAction(Request $request) : Response
   {
      $result = $this->queryBus->sendWithRouting(
         "ticket.getById",
         $request->get("ticketId"),
         // Tell Ecotone which format you want in return
         expectedReturnedMediaType: "application/json"            
      );
      
      return new Response($result);
   }
}
```

{% endtab %}

{% tab title="Lite" %}

```php
$ticket = $messagingSystem->getQueryBus()->sendWithRouting(
   "ticket.getById",
   $ticketId,
   expectedReturnedMediaType: "application/json"            
);
```

{% endtab %}
{% endtabs %}

## C


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ecotone.tech/modelling/command-handling/external-command-handlers/query-handling.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
