How, in Yii, to get the current page\'s URL. For example:
http://www.yoursite.com/your_yii_application/?lg=pl&id=15
but excluding the
Most of the answers are wrong.
The Question is to get url without some query param .
Here is the function that works. It does more things actually. You can remove the param that you don't want and you can add or modify an existing one.
/**
* Function merges the query string values with the given array and returns the new URL
* @param string $route
* @param array $mergeQueryVars
* @param array $removeQueryVars
* @return string
*/
public static function getUpdatedUrl($route = '', $mergeQueryVars = [], $removeQueryVars = [])
{
$currentParams = $request = Yii::$app->request->getQueryParams();
foreach($mergeQueryVars as $key=> $value)
{
$currentParams[$key] = $value;
}
foreach($removeQueryVars as $queryVar)
{
unset($currentParams[$queryVar]);
}
$currentParams[0] = $route == '' ? Yii::$app->controller->getRoute() : $route;
return Yii::$app->urlManager->createUrl($currentParams);
}
usage:
ClassName:: getUpdatedUrl('',[],['remove_this1','remove_this2'])
This will remove query params 'remove_this1' and 'remove_this2' from URL and return you the new URL
Yii::app()->createAbsoluteUrl(Yii::app()->request->url)
This will output something in the following format:
http://www.yoursite.com/your_yii_application/
Yii2
Url::current([], true);
or
Url::current();