How to Get Drupal8 Current URL Path Using PHP

Get your current path (and more) from Drupal8
Get your current path (and more) from Drupal8

I have a feeling this is going to be a popular request, so I wanted to get it out onto the web:


To get the current PATH from Drupal 8 using PHP:

print \Drupal::request()->getRequestUri();



But WAIT! You look like someone that wants to get your site URL too - the "HTTP Host" as it were:

print \Drupal::request()->getHttpHost();



...and here are the methods provided by \Drupal::request() as of Drupal 8 Release Candidate 3:

    [0] => __construct
    [1] => initialize
    [2] => createFromGlobals
    [3] => create
    [4] => setFactory
    [5] => duplicate
    [6] => __clone
    [7] => __toString
    [8] => overrideGlobals
    [9] => setTrustedProxies
    [10] => getTrustedProxies
    [11] => setTrustedHosts
    [12] => getTrustedHosts
    [13] => setTrustedHeaderName
    [14] => getTrustedHeaderName
    [15] => normalizeQueryString
    [16] => enableHttpMethodParameterOverride
    [17] => getHttpMethodParameterOverride
    [18] => get
    [19] => getSession
    [20] => hasPreviousSession
    [21] => hasSession
    [22] => setSession
    [23] => getClientIps
    [24] => getClientIp
    [25] => getScriptName
    [26] => getPathInfo
    [27] => getBasePath
    [28] => getBaseUrl
    [29] => getScheme
    [30] => getPort
    [31] => getUser
    [32] => getPassword
    [33] => getUserInfo
    [34] => getHttpHost
    [35] => getRequestUri
    [36] => getSchemeAndHttpHost
    [37] => getUri
    [38] => getUriForPath
    [39] => getRelativeUriForPath
    [40] => getQueryString
    [41] => isSecure
    [42] => getHost
    [43] => setMethod
    [44] => getMethod
    [45] => getRealMethod
    [46] => getMimeType
    [47] => getFormat
    [48] => setFormat
    [49] => getRequestFormat
    [50] => setRequestFormat
    [51] => getContentType
    [52] => setDefaultLocale
    [53] => getDefaultLocale
    [54] => setLocale
    [55] => getLocale
    [56] => isMethod
    [57] => isMethodSafe
    [58] => getContent
    [59] => getETags
    [60] => isNoCache
    [61] => getPreferredLanguage
    [62] => getLanguages
    [63] => getCharsets
    [64] => getEncodings
    [65] => getAcceptableContentTypes
    [66] => isXmlHttpRequest

Tags: