Piwik\
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.
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();
}
The class defines the following methods:
getCurrentUrl()
— Returns the current URL.getCurrentUrlWithoutQueryString()
— Returns the current URL without the query string.getCurrentUrlWithoutFileName()
— Returns the current URL without the query string and without the name of the file being executed.getCurrentScriptPath()
— Returns the path to the script being executed.getCurrentScriptName()
— Returns the path to the script being executed.getCurrentScheme()
— Returns the current URL's protocol.getCurrentHost()
— Returns the current host.getCurrentQueryString()
— Returns the query string of the current URL.getArrayFromCurrentQueryString()
— Returns an array mapping query parameter names with query parameter values for the current URL.getCurrentQueryStringWithParametersModified()
— Modifies the current query string with the supplied parameters and returns the result.getQueryStringFromParameters()
— Converts an array of parameters name => value mappings to a query string.redirectToReferrer()
— Redirects the user to the referrer.redirectToUrl()
— Redirects the user to the specified URL.getReferrer()
— Returns the HTTP_REFERER $_SERVER
variable, or false
if not found.isLocalUrl()
— Returns true
if the URL points to something on the same host, false
if otherwise.getCurrentUrl()
Returns the current URL.
string
—
eg, "http://example.org/dir1/dir2/index.php?param1=value1¶m2=value2"
getCurrentUrlWithoutQueryString()
Returns the current URL without the query string.
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¶m2=value2"
.
getCurrentUrlWithoutFileName()
Returns the current URL without the query string and without the name of the file being executed.
string
—
eg, "http://example.org/dir1/dir2/"
if the current URL is
"http://example.org/dir1/dir2/index.php?param1=value1¶m2=value2"
.getCurrentScriptPath()
Returns the path to the script being executed. The script file name is not included.
string
—
eg, "/dir1/dir2/"
if the current URL is
"http://example.org/dir1/dir2/index.php?param1=value1¶m2=value2"
getCurrentScriptName()
Returns the path to the script being executed. Includes the script file name.
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¶m2=value2"
getCurrentScheme()
Returns the current URL's protocol.
string
—
'https'
or 'http'
getCurrentHost()
Returns the current host.
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¶m2=value2"
getCurrentQueryString()
Returns the query string of the current URL.
string
—
eg, "?param1=value1¶m2=value2"
if the current URL is
"http://example.org/dir1/dir2/index.php?param1=value1¶m2=value2"
getArrayFromCurrentQueryString()
Returns an array mapping query parameter names with query parameter values for the current URL.
Returns: array
—
If current URL is "http://example.org/dir1/dir2/index.php?param1=value1¶m2=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.
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¶m3=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.
It accepts the following parameter(s):
$parameters
(array
) —
eg. array('param1' => 10, 'param2' => array(1,2))
Returns: string
—
eg. "param1=10¶m2[]=1¶m2[]=2"
redirectToReferrer()
Redirects the user to the referrer. If no referrer exists, the user is redirected to the current URL without query string.
redirectToUrl()
Redirects the user to the specified URL.
It accepts the following parameter(s):
$url
(string
) —It does not return anything or a mixed result.
getReferrer()
Returns the HTTP_REFERER $_SERVER
variable, or false
if not found.
string
|false
—isLocalUrl()
Returns true
if the URL points to something on the same host, false
if otherwise.
It accepts the following parameter(s):
$url
(string
) —Returns: bool
—
True if local; false otherwise.