You are here

public static function Url::createFromRequest in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Url.php \Drupal\Core\Url::createFromRequest()
  2. 9 core/lib/Drupal/Core/Url.php \Drupal\Core\Url::createFromRequest()

Returns the Url object matching a request.

SECURITY NOTE: The request path is not checked to be valid and accessible by the current user to allow storing and reusing Url objects by different users. The 'path.validator' service getUrlIfValid() method should be used instead of this one if validation and access check is desired. Otherwise, 'access_manager' service checkNamedRoute() method should be used on the router name and parameters stored in the Url object returned by this method.

Parameters

\Symfony\Component\HttpFoundation\Request $request: A request object.

Return value

static A Url object. Warning: the object is created even if the current user would get an access denied running the same request via the normal page flow.

Throws

\Drupal\Core\Routing\MatchingRouteNotFoundException Thrown when the request cannot be matched.

3 calls to Url::createFromRequest()
OEmbedIframeController::render in core/modules/media/src/Controller/OEmbedIframeController.php
Renders an oEmbed resource.
UnroutedUrlTest::testCreateFromRequest in core/tests/Drupal/Tests/Core/UnroutedUrlTest.php
Tests the createFromRequest method.
ViewEditForm::submitDelayDestination in core/modules/views_ui/src/ViewEditForm.php
Submit handler for form buttons that do not complete a form workflow.

File

core/lib/Drupal/Core/Url.php, line 488

Class

Url
Defines an object that holds information about a URL.

Namespace

Drupal\Core

Code

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);
}