8.5.x Url.php Url::fromRoute($route_name, $route_parameters = [], $options = [])
8.0.x Url.php Url::fromRoute($route_name, $route_parameters = array(), $options = array())
8.1.x Url.php Url::fromRoute($route_name, $route_parameters = array(), $options = array())
8.2.x Url.php Url::fromRoute($route_name, $route_parameters = array(), $options = array())
8.3.x Url.php Url::fromRoute($route_name, $route_parameters = [], $options = [])
8.4.x Url.php Url::fromRoute($route_name, $route_parameters = [], $options = [])
8.6.x Url.php 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.


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



212 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


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


Defines an object that holds information about a URL.




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


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:

  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.' ;

glass.dimly’s picture

That four-liner makes me miss deprecated l() already.

megan_m’s picture

The part in the curly brackets in the routing.yml file is the key to the array. Here's another example for a contact form:

In contact.routing.yml:

  path: '/contact/{contact_form}'

To get a URL to the contact form, we use contact_form as the array key:

  $url = Url::fromRoute('entity.contact_form.canonical', array('contact_form' => 'machine_name_of_form'));
asghar’s picture

We can create html link from argument routing like this

  path: '/mypage/{mypage_id}'
    _title: 'My Page'
    # Page callback. Method that renders your page.
    _controller: '\Drupal\mypage\Controller\MyPageController::content'
    _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);
ElusiveMind’s picture

ExTexan’s picture

I see examples above of args at the end of the URL, but what about one embedded in the middle?...

Like /path/to/{arg_0}/my/view

I've tried every different syntax I can think of, but all have returned "Route xxxxxx does not exist."