Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

As a part of the new New Symfony-based routing system, Drupal logic for generating a URL from a path or route needs to be integrated with the symfony HttpKernel.

Drupal 7

  $url = url($path, $options);

Drupal 8

The case of creating a URL from a route name:

  $generator = Drupal::urlGenerator();
  $route_name = 'test_1';
  $url = $generator->generate($route_name);

The case of generating a URL from a route name with parameters. The route may require parameters to build the URL, depending on the path pattern

  $generator = Drupal::urlGenerator();
  $url = $generator->generateFromRoute('entity.user.edit_form', array('user'  => '99'));

note, the pattern for this route is /user/{user}/edit

The case of getting the internal path (path relative to the site's base URL) corresponding to a route name.

  $generator = Drupal::urlGenerator();
  $route_name = 'test_1';
  $path = $generator->getPathFromRoute($route_name);

Note that the generator is available from the DIC as 'url_generator'. A global helper method is available at Drupal::urlGenerator(). which simply pulls the object from the container.

Using routes is recommended. Paths are deprecated, but for situations where you have a path as user input you can match it to a route, or simple render as a relative URL using the Url class:

  $url = \Drupal\Core\Url::fromUserInput($path, $options);
  $output = $url->toString();
Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done