You are here

public static function Url::createFromRequest in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Url.php \Drupal\Core\Url::createFromRequest()
  2. 10 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.

6 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.
UrlTest::testCreateFromRequest in core/tests/Drupal/Tests/Core/UrlTest.php
Tests the createFromRequest method.
UrlTest::testUrlFromRequest in core/tests/Drupal/Tests/Core/UrlTest.php
Tests creating a Url from a request.
UrlTest::testUrlFromRequestInvalid in core/tests/Drupal/Tests/Core/UrlTest.php
Tests that an invalid request will thrown an exception.

... See full list

File

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

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