matchRequest example

/** * @return array<string> */
    public function warmUp(string $cacheDir): array
    {
        return $this->decorated->warmUp($cacheDir);
    }

    public function matchRequest(Request $request): array
    {
        if (!$request->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID)) {
            return $this->decorated->matchRequest($request);
        }

        $server = array_merge(
            $request->server->all(),
            ['REQUEST_URI' => $request->attributes->get(RequestTransformer::SALES_CHANNEL_RESOLVED_URI)]
        );

        $localClone = $request->duplicate(null, null, null, null, null, $server);

        return $this->decorated->matchRequest($localClone);
    }

    
return $this->getMatcher()->match($pathinfo);
    }

    public function matchRequest(Request $request): array
    {
        $matcher = $this->getMatcher();
        if (!$matcher instanceof RequestMatcherInterface) {
            // fallback to the default UrlMatcherInterface             return $matcher->match($request->getPathInfo());
        }

        return $matcher->matchRequest($request);
    }

    /** * Gets the UrlMatcher or RequestMatcher instance associated with this Router. */
    public function getMatcher(): UrlMatcherInterface|RequestMatcherInterface
    {
        if (isset($this->matcher)) {
            return $this->matcher;
        }

        

  public static function createFromRequest(Request $request) {
    // We use the router without access checks because URL objects might be     // created and stored for different users.     $result = \Drupal::service('router.no_access_checks')->matchRequest($request);
    $route_name = $result[RouteObjectInterface::ROUTE_NAME];
    $route_parameters = $result['_raw_variables']->all();
    return new static($route_name$route_parameters);
  }

  /** * Sets this URL to encapsulate an unrouted URI. * * @return $this */
  protected function setUnrouted() {
    
$this->setCurrentRequest($request);

        if ($request->attributes->has('_controller')) {
            // routing is already done             return;
        }

        // add attributes based on the request (routing)         try {
            // matching a request is more powerful than matching a URL path + context, so try that first             if ($this->matcher instanceof RequestMatcherInterface) {
                $parameters = $this->matcher->matchRequest($request);
            } else {
                $parameters = $this->matcher->match($request->getPathInfo());
            }

            $this->logger?->info('Matched route "{route}".', [
                'route' => $parameters['_route'] ?? 'n/a',
                'route_parameters' => $parameters,
                'request_uri' => $request->getUri(),
                'method' => $request->getMethod(),
            ]);

            
/** * Tests the matchRequest() function for access allowed. */
  public function testMatchRequestAllowed() {
    $this->setupRouter();
    $request = new Request();
    $access_result = AccessResult::allowed();
    $this->accessManager->expects($this->once())
      ->method('checkRequest')
      ->with($request)
      ->willReturn($access_result);
    $parameters = $this->accessAwareRouter->matchRequest($request);
    $expected = [
      RouteObjectInterface::ROUTE_OBJECT => $this->route,
      AccessAwareRouterInterface::ACCESS_RESULT => $access_result,
    ];
    $this->assertSame($expected$request->attributes->all());
    $this->assertSame($expected$parameters);
  }

  /** * Tests the matchRequest() function for access denied. */
  
return $this->getMatcher()->match($pathinfo);
    }

    public function matchRequest(Request $request): array
    {
        $matcher = $this->getMatcher();
        if (!$matcher instanceof RequestMatcherInterface) {
            // fallback to the default UrlMatcherInterface             return $matcher->match($request->getPathInfo());
        }

        return $matcher->matchRequest($request);
    }

    /** * Gets the UrlMatcher or RequestMatcher instance associated with this Router. */
    public function getMatcher(): UrlMatcherInterface|RequestMatcherInterface
    {
        if (null !== $this->matcher) {
            return $this->matcher;
        }

        
public function addDeprecatedRouteEnhancer(EnhancerInterface $route_enhancer) {
    @trigger_error('non_lazy_route_enhancer is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use route_enhancer instead. See https://www.drupal.org/node/2894934', E_USER_DEPRECATED);
    $this->enhancers[] = $route_enhancer;
  }

  /** * {@inheritdoc} */
  public function match($pathinfo): array {
    $request = Request::create($pathinfo);

    return $this->matchRequest($request);
  }

  /** * {@inheritdoc} */
  public function matchRequest(Request $request): array {
    $collection = $this->getInitialRouteCollection($request);
    if ($collection->count() === 0) {
      throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $this->currentPath->getPath()));
    }
    $collection = $this->applyRouteFilters($collection$request);
    
public function getContext(): SymfonyRequestContext {
    return $this->router->getContext();
  }

  /** * {@inheritdoc} * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException * Thrown when access checking failed. */
  public function matchRequest(Request $request): array {
    $parameters = $this->router->matchRequest($request);
    $request->attributes->add($parameters);
    $this->checkAccess($request);
    // We can not return $parameters because the access check can change the     // request attributes.     return $request->attributes->all();
  }

  /** * Apply access check service to the route and parameters in the request. * * @param \Symfony\Component\HttpFoundation\Request $request * The request to access check. */

  protected static $modules = [
    'system',
  ];

  /** * Test that the negotiator applies to the batch route. */
  public function testApplies() {
    $request = Request::create('/batch');
    // Use the router to enhance the object so that a RouteMatch can be created.     $this->container->get('router')->matchRequest($request);
    $routeMatch = RouteMatch::createFromRequest($request);
    // The negotiator under test.     $negotiator = $this->container->get('theme.negotiator.system.batch');

    $this->assertTrue($negotiator->applies($routeMatch));
  }

}
$absolutePath = $this->router->generate($case->route);
        $absoluteUrl = $this->router->generate($case->route, [], Router::ABSOLUTE_URL);
        $networkPath = $this->router->generate($case->route, [], Router::NETWORK_PATH);
        $pathInfo = $this->router->generate($case->route, [], Router::PATH_INFO);

        static::assertSame($case->getAbsolutePath()$absolutePathvar_export($case, true));
        static::assertSame($case->getAbsoluteUrl()$absoluteUrlvar_export($case, true));
        static::assertSame($case->getNetworkPath()$networkPathvar_export($case, true));
        static::assertSame($case->getPathInfo()$pathInfovar_export($case, true));

        $matches = $this->router->matchRequest($transformedRequest);
        static::assertEquals($case->route, $matches['_route']);

        $matches = $this->router->match($transformedRequest->getPathInfo());
        static::assertEquals($case->route, $matches['_route']);

        // test seo url generation         $host = $transformedRequest->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL)
            . $transformedRequest->attributes->get(RequestTransformer::SALES_CHANNEL_BASE_URL);

        $absoluteSeoUrl = $this->seoUrlReplacer->replace(
            $this->seoUrlReplacer->generate(
                
$this->assertNotInstanceOf(CompiledUrlGenerator::class$this->router->getGenerator());
    }

    public function testMatchRequestWithUrlMatcherInterface()
    {
        $matcher = $this->createMock(UrlMatcherInterface::class);
        $matcher->expects($this->once())->method('match');

        $p = new \ReflectionProperty($this->router, 'matcher');
        $p->setValue($this->router, $matcher);

        $this->router->matchRequest(Request::create('/'));
    }

    public function testMatchRequestWithRequestMatcherInterface()
    {
        $matcher = $this->createMock(RequestMatcherInterface::class);
        $matcher->expects($this->once())->method('matchRequest');

        $p = new \ReflectionProperty($this->router, 'matcher');
        $p->setValue($this->router, $matcher);

        $this->router->matchRequest(Request::create('/'));
    }

    public function checkRequestPath(Request $request, string $path): bool
    {
        if ('/' !== $path[0]) {
            try {
                // matching a request is more powerful than matching a URL path + context, so try that first                 if ($this->urlMatcher instanceof RequestMatcherInterface) {
                    $parameters = $this->urlMatcher->matchRequest($request);
                } else {
                    $parameters = $this->urlMatcher->match($request->getPathInfo());
                }

                return isset($parameters['_route']) && $path === $parameters['_route'];
            } catch (MethodNotAllowedException) {
                return false;
            } catch (ResourceNotFoundException) {
                return false;
            }
        }

        
$this->setCurrentRequest($request);

        if ($request->attributes->has('_controller')) {
            // routing is already done             return;
        }

        // add attributes based on the request (routing)         try {
            // matching a request is more powerful than matching a URL path + context, so try that first             if ($this->matcher instanceof RequestMatcherInterface) {
                $parameters = $this->matcher->matchRequest($request);
            } else {
                $parameters = $this->matcher->match($request->getPathInfo());
            }

            $this->logger?->info('Matched route "{route}".', [
                'route' => $parameters['_route'] ?? 'n/a',
                'route_parameters' => $parameters,
                'request_uri' => $request->getUri(),
                'method' => $request->getMethod(),
            ]);

            
    // AcceptHeaderMatcher when matching the request.     $request->headers->set('Accept', 'text/html');
    // Find the system path by resolving aliases, language prefix, etc.     $processed = $this->pathProcessor->processInbound($path$request);
    if (empty($processed) || !empty($exclude[$processed])) {
      // This resolves to the front page, which we already add.       return NULL;
    }
    $this->currentPath->setPath($processed$request);
    // Attempt to match this path to provide a fully built request.     try {
      $request->attributes->add($this->router->matchRequest($request));
      return $request;
    }
    catch (ParamNotConvertedException $e) {
      return NULL;
    }
    catch (ResourceNotFoundException $e) {
      return NULL;
    }
    catch (MethodNotAllowedException $e) {
      return NULL;
    }
    
Home | Imprint | This part of the site doesn't use cookies.