Get current URL/URI without some of $_GET variables

Get current URL/URI without some of $_GET variables

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Cleaning Up URLs in Yii Without String Manipulation

When building a navigation system or generating links for a user interface, you often encounter a specific friction point: the current request URL includes query parameters that shouldn't be part of the canonical path. Consider a scenario where your application state is passed via ?lg=pl&id=15. If you simply echo $this->request->url, you get the full string including those parameters. However, if you attempt to generate a clean link using CHtml::link() or similar helpers, the framework might inadvertently append these existing GET variables again, resulting in duplicate parameters or stale state.

In Yii 1.x, the standard approach involves manipulating the global $_GET array directly or reconstructing the URL from the route components. The snippet provided in community discussions often looks like this:

unset($_GET['lg']);

echo Yii::app()->createUrl(
    Yii::app()->controller->getId() . '/' . Yii::app()->controller->getAction()->getId(), 
    $_GET
);

While functional, this approach relies on the side effect of unset() modifying a global variable. This creates a subtle coupling between your URL generation logic and the raw PHP superglobal state. It implies that for the duration of that specific request, the application no longer knows about the lg parameter unless you re-introduce it elsewhere. If any other controller action or filter depends on $this->request->getParam('lg'), this local unset could cause unexpected behavior downstream in the same request lifecycle.

A more robust architectural pattern leverages Yii's CRequest object directly rather than mutating $_GET. The CRequest class provides a dedicated method, getBaseUrl(), which returns the URL without any query string. By combining this with the route components, you can construct a clean URI without touching the global state.

Here is how you might refactor the logic to be safer and more explicit:

$baseUrl = Yii::app()->request->baseUrl;
$route = Yii::app()->controller->id . '/' . Yii::app()->controller->action->id;

// Construct the clean URL manually or pass an empty array for query string if needed
$cleanUrl = $baseUrl . '?' . http_build_query([]) . $route; 

However, a cleaner implementation usually involves using createUrl with an empty query array or selectively building the parameter list. The key distinction here is that you are not altering $_GET; you are explicitly defining which parameters should accompany the route. This separates the "current state" (which includes all GET vars) from the "canonical representation" (which excludes specific vars).

If your goal is to generate a link that ignores specific variables like lg but preserves others like id, you can pass a filtered array to createUrl. This ensures the framework handles the URL encoding and routing logic correctly, preventing double-append issues where Yii might try to re-add default parameters.

$cleanParams = array_diff_key($_GET, array('lg' => ''));
$url = Yii::app()->createUrl(Yii::app()->controller->id . '/' . Yii::app()->controller->action->id, $cleanParams);

This approach is preferable because it treats query parameters as data to be managed, rather than relying on global variable mutation. It makes the intent explicit: "I want all GET variables except 'lg'."

If you are working within a Laravel ecosystem instead of legacy Yii, this problem is solved differently due to the request lifecycle management. In Laravel, you do not need to unset $_GET or manually construct the URL from components. The request() helper provides a url() method that accepts an array of query parameters. You can simply pass a new array containing only the keys you wish to retain, and the framework will generate the correct URI.

$url = url(route(), ['lg' => null]); 
// Or more explicitly:
$url = url($route, array_filter($_GET, function($key) { return $key !== 'lg'; }));

While Yii requires you to be careful about how you pass parameters to avoid appending them twice, Laravel's url() helper intelligently merges the provided query string with the existing path without duplicating keys. This reduces the cognitive load on the developer regarding URL reconstruction logic.

Our network