Piwik\

Url

Provides URL related helper methods.

This class provides simple methods that can be used to parse and modify the current URL. It is most useful when plugins need to redirect the current request to a URL and when they need to link to other parts of Matomo (formerly Piwik) in HTML.

Examples

Redirect to a different controller action

public function myControllerAction()
{
    $url = Url::getCurrentQueryStringWithParametersModified(array(
        'module' => 'DevicesDetection',
        'action' => 'index'
    ));
    Url::redirectToUrl($url);
}

Link to a different controller action in a template

public function myControllerAction()
{
    $url = Url::getCurrentQueryStringWithParametersModified(array(
        'module' => 'UserCountryMap',
        'action' => 'realtimeMap',
        'changeVisitAlpha' => 0,
        'removeOldVisits' => 0
    ));
    $view = new View("@MyPlugin/myPopup");
    $view->realtimeMapUrl = $url;
    return $view->render();
}

Methods

The class defines the following methods:

getCurrentUrl()

Returns the current URL.

Signature

  • Returns: string — eg, "http://example.org/dir1/dir2/index.php?param1=value1&param2=value2"

getCurrentUrlWithoutQueryString()

Returns the current URL without the query string.

Signature

  • It accepts the following parameter(s):

    • $checkTrustedHost (bool) — Whether to do trusted host check. Should ALWAYS be true, except in Controller.
  • Returns: string — eg, "http://example.org/dir1/dir2/index.php" if the current URL is "http://example.org/dir1/dir2/index.php?param1=value1&param2=value2".

getCurrentUrlWithoutFileName()

Returns the current URL without the query string and without the name of the file being executed.

Signature

  • Returns: string — eg, "http://example.org/dir1/dir2/" if the current URL is "http://example.org/dir1/dir2/index.php?param1=value1&param2=value2".

getCurrentScriptPath()

Returns the path to the script being executed. The script file name is not included.

Signature

  • Returns: string — eg, "/dir1/dir2/" if the current URL is "http://example.org/dir1/dir2/index.php?param1=value1&param2=value2"

getCurrentScriptName()

Returns the path to the script being executed. Includes the script file name.

Signature

  • It accepts the following parameter(s):

    • $removePathInfo (bool) — If true (default value) then the PATH_INFO will be stripped.
  • Returns: string — eg, "/dir1/dir2/index.php" if the current URL is "http://example.org/dir1/dir2/index.php?param1=value1&param2=value2"

getCurrentScheme()

Returns the current URL's protocol.

Signature

  • Returns: string'https' or 'http'

getCurrentHost()

Returns the current host.

Signature

  • It accepts the following parameter(s):

    • $default (string) — Default value to return if host unknown
    • $checkTrustedHost (bool) — Whether to do trusted host check. Should ALWAYS be true, except in Controller.
  • Returns: string — eg, "example.org" if the current URL is "http://example.org/dir1/dir2/index.php?param1=value1&param2=value2"

getCurrentQueryString()

Returns the query string of the current URL.

Signature

  • Returns: string — eg, "?param1=value1&param2=value2" if the current URL is "http://example.org/dir1/dir2/index.php?param1=value1&param2=value2"

getArrayFromCurrentQueryString()

Returns an array mapping query parameter names with query parameter values for the current URL.

Signature

  • Returns: array — If current URL is "http://example.org/dir1/dir2/index.php?param1=value1&param2=value2" this will return:

              array(
                  'param1' => string 'value1',
                  'param2' => string 'value2'
              )
    

getCurrentQueryStringWithParametersModified()

Modifies the current query string with the supplied parameters and returns the result. Parameters in the current URL will be overwritten with values in $params and parameters absent from the current URL but present in $params will be added to the result.

Signature

  • It accepts the following parameter(s):

    • $params (array) — set of parameters to modify/add in the current URL eg, array('param3' => 'value3')
  • Returns: string — eg, "?param2=value2&param3=value3"

getQueryStringFromParameters()

Converts an array of parameters name => value mappings to a query string. Values must already be URL encoded before you call this function.

Signature

  • It accepts the following parameter(s):

    • $parameters (array) — eg. array('param1' => 10, 'param2' => array(1,2))
  • Returns: string — eg. "param1=10&param2[]=1&param2[]=2"

redirectToReferrer()

Redirects the user to the referrer. If no referrer exists, the user is redirected to the current URL without query string.

Signature

  • It does not return anything or a mixed result.

redirectToUrl()

Redirects the user to the specified URL.

Signature

  • It accepts the following parameter(s):

    • $url (string) —
  • It does not return anything or a mixed result.

  • It throws one of the following exceptions:

getReferrer()

Returns the HTTP_REFERER $_SERVER variable, or false if not found.

Signature

  • Returns: string|false

isLocalUrl()

Returns true if the URL points to something on the same host, false if otherwise.

Signature

  • It accepts the following parameter(s):

    • $url (string) —
  • Returns: bool — True if local; false otherwise.