Skip to main content
1 of 5
rphair
  • 169
  • 13

getting URL from Route: breaks in localhost subdirectory

I just had a Drupal 8 site break when moving it from a top-level domain on a server to a subdirectory on my desktop machine: apparently that subdirectory wants to be included in the URL as converted from routes. This threatens to ruin my sandbox environment so I would like to figure out 1) why this is happening and/or 2) what isn't portable about my code fragment below. I've been using

Url::fromRouteMatch($route_match)->toString()

to get the URL Alias of the current page: e.g., the RouteMatch passed as argument to the breadcrumb service. On the server, this returns a proper pathname relative to the top-level domain, for instance:

/dir1/dir2/dir3/myAlias

... but running the same code in my sandbox (http://localhost/mySite) includes the subdirectory name, as if Drupal is returning the URL relative my local web server's document root, instead of the root of my Drupal site:

/mySite/dir1/dir2/dir3/myAlias

I realise there may be better ways to get the current URL Alias (e.g., \Drupal::service('path.current')->getPath() and then passing it to \Drupal::service('path.alias_manager')->getAliasByPath, but I'd really like to convert the route itself since that's the argument to the breadcrumb builder in Drupal 8.

rphair
  • 169
  • 13