Get the title from a Url in Drupal
A collection of mugs with names on it

As I was building a module to create bookmark in Drupal, I needed to find the best way to retrieve the page title for any given Url possible (e.g. path aliases).

It turns out there is a resolver service for that: the title_resolver.

However it is not obvious how to use it. There are just a few implementation in core and none is easy to duplicate and use as-is in your own module. So I'd like to share my solution.

Let's say you want the title of a page but you just know its path alias - such as /my-custom-page-alias - you need to load the Url from the path, get a route match, create a request and load both into the Title resolver... and boom! You get the page title.