Working with links
One of the principle characteristics of a web application is the myriad of links between its resources. They are in fact the glue that brings it together. So, in this section, I want to show you a few common techniques used while working with links programmatically in Drupal 8.
There are two main aspects when talking about link building in Drupal--the URL and the actual link tag itself. So, creating a link involves a two-step process that deals with these two, but can also be shortened into a single call via some helper methods.
The URL
URLs in Drupal 8 are represented with the Drupal\Core\Url
class, which has a number of static methods that allow you to create an instance. The most important of these is ::fromRoute()
, which takes a route name, route parameters (if any are needed for that route), and an array of options to create a new instance of Url
. There are other such methods available that turn all sorts of other things into a Url
, most notably, the ::fromUri()
method...