Get current URL/URI without some of $_GET variables

后端 未结 15 1919
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 09:10

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

相关标签:
15条回答
  • 2021-01-30 09:27

    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

    0 讨论(0)
  • 2021-01-30 09:29
    Yii::app()->createAbsoluteUrl(Yii::app()->request->url)
    

    This will output something in the following format:

    http://www.yoursite.com/your_yii_application/
    
    0 讨论(0)
  • 2021-01-30 09:32

    Yii2

    Url::current([], true);
    

    or

    Url::current();
    
    0 讨论(0)
提交回复
热议问题