$route->getName() returns null for ajax call

I wrote a middleware that returns defined translations. This is done by this piece of the code
MiddlewareTranslations.php

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  {
      $routeContext = RouteContext::fromRequest($request);
      $route = $routeContext->getRoute();

// first parameter of getTranslations() determines the file name, in which translations are kepts
      $phrases = $this->i18n->getTranslations($route->getName(), $this->i18n->get_locale());

      $request = $request->withAttribute('i18n', $phrases);

      return $handler->handle($request);
  }

That works just fine whenever the request is generated by the browser. This is confirmed by the entry that is written in the log file each time such request is registered:
[2021-12-02T10:29:15.460261+01:00] translation.DEBUG: Obtaining translations for the route: "dashboard"; /dashboard GET-200

Once the page is loaded, separate elements of the page are pulled out by the ajax calls.
Example:

var request = $.ajax({
        url:		"/dashboard/graph/" + graphID,
        method:		"GET",
        dataType:	"html",
      });

all calls are aligned with defined routes:

$app->get('/dashboard', \App\Action\Dashboard::class)->setName('dashboard');
$app->get('/dashboard/graph/{name}', \App\Action\DashboardGraph::class . ':load');

Surprisingly, $route->getName() from MiddlewareTranslations::process() returns null, what is also reflected in the log file:

[2021-12-02T10:29:15.460261+01:00] translation.DEBUG: Obtaining translations for the route: ""; / GET-200  
[2021-12-02T09:54:31.584726+01:00] app.DEBUG: Ajax request captured. Processing request for graph: budget_balance; / GET-200

Does anyone know why it is the case? Regardless what url in ajax I call (FQDN or not), the result is the same? What am I missing here?

Much appreciate for any help.

I guess the name is null because the route name is not defined in all routes, like this:

$app->get('/dashboard/graph/{name}', \App\Action\DashboardGraph::class . ':load');
1 Like
$app->get('/graph/{name}', \App\Action\DashboardGraph::class . ':load')->setName('dashboard');

did the trick