8.3.x Url.php public static Url::fromRoute($route_name, $route_parameters = [], $options = [])
8.0.x Url.php public static Url::fromRoute($route_name, $route_parameters = array(), $options = array())
8.1.x Url.php public static Url::fromRoute($route_name, $route_parameters = array(), $options = array())
8.2.x Url.php public static Url::fromRoute($route_name, $route_parameters = array(), $options = array())
8.4.x Url.php public static Url::fromRoute($route_name, $route_parameters = [], $options = [])

Creates a new Url object for a URL that has a Drupal route.

This method is for URLs that have Drupal routes (that is, most pages generated by Drupal). For non-routed local URIs relative to the base path (like robots.txt) use Url::fromUri() with the base: scheme.

Parameters

string $route_name: The name of the route

array $route_parameters: (optional) An associative array of route parameter names and values.

array $options: See \Drupal\Core\Url::fromUri() for details.

Return value

\Drupal\Core\Url A new Url object for a routed (internal to Drupal) URL.

See also

\Drupal\Core\Url::fromUserInput()

\Drupal\Core\Url::fromUri()

291 calls to Url::fromRoute()
AggregatorController::adminOverview in core/modules/aggregator/src/Controller/AggregatorController.php
Displays the aggregator administration page.
AjaxFormCacheTest::testFormCacheUsage in core/modules/system/src/Tests/Ajax/AjaxFormCacheTest.php
Tests the usage of form cache for AJAX forms.
AjaxFormCacheTest::testQueryString in core/modules/system/src/Tests/Ajax/AjaxFormCacheTest.php
Tests AJAX forms on pages with a query string.
AjaxTestController::dialog in core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php
Returns a render array of form elements and links for dialog.
AjaxTestController::dialogContents in core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php
Example content for dialog testing.

... See full list

File

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

Class

Url
Defines an object that holds information about a URL.

Namespace

Drupal\Core

Code

public static function fromRoute($route_name, $route_parameters = array(), $options = array()) {
  return new static($route_name, $route_parameters, $options);
}

Comments

mlncn’s picture

The available route names are defined in module *.routing.yml files.

The parameters should be passed in as an associative array.

Given this in the example.routing.yml:

entity.give_form.donate:
  path: '/give/{give_form}/{give_donation}'

We can pass a URL object into a form's redirect URL like this:

$form_state->setRedirectUrl(Url::fromRoute('entity.give_form.donate', ['give_form' => $give_donation->get('give_form')->target_id, 'give_donation' => $give_donation->id()]));

Also welcome to modern PHP and the bracket [] replacement for array() syntax!

ransomweaver’s picture

A view route with a param, where the page path is /brands/%

$url = Url::fromRoute('view.brands.page_1', array('arg_0' => $retail_store->id()));
$link = Link::fromTextAndUrl(t('Brand Management'), $url);
$link = $link->toRenderable();
$status_str = 'Your store is approved!. Visit ' . render($link) . ' to find brands.' ;

asghar’s picture

We can create html link from argument routing like this

mypage.view:
  path: '/mypage/{mypage_id}'
  defaults:
    _title: 'My Page'
    # Page callback. Method that renders your page.
    _controller: '\Drupal\mypage\Controller\MyPageController::content'
  requirements:
    _permission: 'view content'
    # You can use regular expressions for correct argument content.
    # In this case, only integers will be available in the page_id the variable, otherwise 404.
    mypage_id: \d+
$url = Url::fromRoute('mypage.view', array(
              'mypage_id' => $item->id
        ));
$link= \Drupal::l($item->id, $url);