title: Implement Middleware in Laminas API Tools
body: <p>So the question is how to connect my middleware to the specific route (or controller) in Laminas API Tools.</p>
  <p>My middleware is:</p>
  <p>EnsureMessageBelongsToTicket.php</p>
  <pre><code>namespace Application\Middleware;
  
  use Fig\Http\Message\StatusCodeInterface;
  use Laminas\Router\RouteMatch;
  use Psr\Http\Message\ResponseFactoryInterface;
  use Psr\Http\Message\ResponseInterface;
  use Psr\Http\Message\ServerRequestInterface;
  use Psr\Http\Server\MiddlewareInterface;
  use Psr\Http\Server\RequestHandlerInterface;
  use SupportApi\V1\Rest\Tickets\TickerRepository;
  
  class EnsureMessageBelongsToTicket extends MiddlewareInterface
  {
      private $_ticketsRepository;
      private $_responseFactory;
  
      public function __construct(TickerRepository $ticketsRepository, ResponseFactoryInterface $responseFactory)
      {
          $this-&gt;_ticketsRepository = $ticketsRepository;
          $this-&gt;_responseFactory = $responseFactory;
      }
  
      public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
      {
  
          dd(123);
          /** @var RouteMatch $routeMatch */
          $routeMatch = $request-&gt;getAttribute(RouteMatch::class);
          $ticketId   = $routeMatch-&gt;getParam('ticket_id');
          $ticket     = $this-&gt;_ticketsRepository-&gt;getTicketById($ticketId);
  
          // if no album was found, we short-circuit the pipe and return a 404 error:
          if ($ticket === null) {
              return $this-&gt;_responseFactory-&gt;createResponse(
                  StatusCodeInterface::STATUS_NOT_FOUND,
                  sprintf('Ticket with ID %s not found!', $ticket)
              );
          }
  
          // ...otherwise we populate the request with the album and call the RequestHandler
          $request = $request-&gt;withAttribute('ticket', $ticket);
          return $handler-&gt;handle($request);
      }
  }
  </code></pre>
  <p>i need to connect this middleware to my ticket resource. I've tried to do this like that in API module.config.php:</p>
  <pre><code>'router' =&gt; [
      'routes' =&gt; [
          'support-api.rest.tickets' =&gt; [
              'type' =&gt; 'Segment',
              'options' =&gt; [
                  'route' =&gt; '/tickets[/:tickets_id]',
                  'defaults' =&gt; [
                      'controller' =&gt; 'SupportApi\\V1\\Rest\\Tickets\\Controller',
                      'middleware' =&gt; EnsureMessageBelongsToTicket::class, &lt;------ thats what i have tried
                  ],
              ],
          ],
          'support-api.rest.messages' =&gt; [
              'type' =&gt; 'Segment',
              'options' =&gt; [
                  'route' =&gt; '/tickets/:ticket_id/messages[/:messages_id]',
                  'defaults' =&gt; [
                      'controller' =&gt; 'SupportApi\\V1\\Rest\\Messages\\Controller',
                  ],
              ],
          ],
      ],
  ],
  </code></pre>
  <p>so is there any other possible ways to do that?</p>