Account created on 6 March 2023, over 1 year ago
#

Recent comments

Got the error (see below) when using the router.no_access_checks service in a custom middleware. The patch from #8 fixes the problem, but I'm not sure whether the patch is the solution or router.no_access_checks should not be used in a middleware.

Handle function in custom middleware which uses the router.no_access_checks service:

public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
  // ...
  try {
    $router = \Drupal::service('router.no_access_checks')->matchRequest($request);
    // ...
  } catch (\Exception $e) {
    // ...
  }
  // ...
  return $this->httpKernel->handle($request, $type, $catch);
}

Core service router.no_access_checks which calls router.request_context:

router.no_access_checks:
  class: \Drupal\Core\Routing\Router
  arguments: ['@router.route_provider', '@path.current', '@url_generator']
  tags:
    # @todo Try to combine those tags together, see https://www.drupal.org/node/2915772.
    - { name: service_collector, tag: non_lazy_route_enhancer, call: addRouteEnhancer }
    - { name: service_collector, tag: route_enhancer, call: addRouteEnhancer  }
    - { name: service_collector, tag: non_lazy_route_filter, call: addRouteFilter }
    - { name: service_collector, tag: route_filter, call: addRouteFilter }
  calls:
    - [setContext, ['@router.request_context']]

Error:

The website encountered an unexpected error. Please try again later.
TypeError: Drupal\Core\Routing\RequestContext::fromRequest(): Argument #1 ($request) must be of type Symfony\Component\HttpFoundation\Request, null given, called in /var/www/html/web/core/lib/Drupal/Core/Routing/RequestContext.php on line 28 in Drupal\Core\Routing\RequestContext->fromRequest() (line 34 of core/lib/Drupal/Core/Routing/RequestContext.php).
Drupal\Core\Routing\RequestContext->fromRequest(NULL) (Line: 28)
Drupal\Core\Routing\RequestContext->fromRequestStack(Object)
call_user_func_array(Array, Array) (Line: 279)
Drupal\Component\DependencyInjection\Container->createService(Array, 'router.request_context') (Line: 176)
Drupal\Component\DependencyInjection\Container->get('router.request_context', 3) (Line: 437)
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 276)
Drupal\Component\DependencyInjection\Container->createService(Array, 'private__C9lAFteypAsjbMduBKjI8pQqjxZI3fGN7cy_1KFyRIk') (Line: 452)
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 240)
Drupal\Component\DependencyInjection\Container->createService(Array, 'url_generator') (Line: 176)
Drupal\Component\DependencyInjection\Container->get('url_generator', 1) (Line: 437)
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 240)
Drupal\Component\DependencyInjection\Container->createService(Array, 'router.no_access_checks') (Line: 176)
Drupal\Component\DependencyInjection\Container->get('router.no_access_checks') (Line: 197)
Drupal::service('router.no_access_checks') (Line: 50)

+1

Would be nice to be able to use multiple coupons from a promotion on one order with the possibility to limit the amount of coupons per order.

In a controller I send a Response and have a fallback RedirectResponse if that or other things didn't work; also because a controller needs to return some sort of response. For some reason this didn't work anymore and I got the described error message.
With the following changes it worked:

Old:

try {
  $service->getResponse($destination, $langcode)->send();
catch {
  // ...
}

return new RedirectResponse($destination, 302);

New:

try {
  $response = $service->getResponse($destination, $langcode);
  return new Response($response->getContent(), $response->getStatusCode(), $response->headers->all());
catch {
  // ...
}

return new RedirectResponse($destination, 302);

PS: I couldn't return the Response directly because it was a TrustedRedirectResponse with an external link which caused whole bunch of other problems.

Production build 0.71.5 2024