Get current URL/URI without some of $_GET variables

134,131

Solution 1

Yii 1

Yii::app()->request->url

For Yii2:

Yii::$app->request->url

Solution 2

Yii::app()->createAbsoluteUrl(Yii::app()->request->url)

This will output something in the following format:

http://www.yoursite.com/your_yii_application/

Solution 3

Yii 1

Most of the other answers are wrong. The poster is asking for the url WITHOUT (some) $_GET-parameters.

Here is a complete breakdown (creating url for the currently active controller, modules or not):

// without $_GET-parameters
Yii::app()->controller->createUrl(Yii::app()->controller->action->id);

// with $_GET-parameters, HAVING ONLY supplied keys
Yii::app()->controller->createUrl(Yii::app()->controller->action->id,
    array_intersect_key($_GET, array_flip(['id']))); // include 'id'

// with all $_GET-parameters, EXCEPT supplied keys
Yii::app()->controller->createUrl(Yii::app()->controller->action->id,
    array_diff_key($_GET, array_flip(['lg']))); // exclude 'lg'

// with ALL $_GET-parameters (as mensioned in other answers)
Yii::app()->controller->createUrl(Yii::app()->controller->action->id, $_GET);
Yii::app()->request->url;

When you don't have the same active controller, you have to specify the full path like this:

Yii::app()->createUrl('/controller/action');
Yii::app()->createUrl('/module/controller/action');

Check out the Yii guide for building url's in general: http://www.yiiframework.com/doc/guide/1.1/en/topics.url#creating-urls

Solution 4

To get the absolute current request url (exactly as seen in the address bar, with GET params and http://) I found that the following works well:

Yii::app()->request->hostInfo . Yii::app()->request->url

Solution 5

In Yii2 you can do:

use yii\helpers\Url;
$withoutLg = Url::current(['lg'=>null], true);

More info: https://www.yiiframework.com/doc/api/2.0/yii-helpers-baseurl#current%28%29-detail

Share:
134,131

Related videos on Youtube

Sebastian
Author by

Sebastian

Updated on July 01, 2021

Comments

  • Sebastian
    Sebastian almost 3 years

    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 $GET_['lg'] (without parsing the string manually)?

    I mean, I'm looking for something similar to the Yii::app()->requestUrl / Chtml::link() methods, for returning URLs minus some of the $_GET variables.

    Edit: Current solution:

    unset $_GET['lg'];
    
    echo Yii::app()->createUrl(
      Yii::app()->controller->getId().'/'.Yii::app()->controller->getAction()->getId() , 
      $_GET 
    );
    
  • Sebastian
    Sebastian over 12 years
    This is really helpful. My only consideration is that Yii is using either the 'normal' (?var=value) format, or PATH (/var/value), they are toggled in a config file. That's why links in Yii are constructed using Chtml::link() with an array of $_GET variables.
  • Sebastian
    Sebastian over 12 years
    Oh ! So I can... give the whole $_GET array as an input parameter, after unsetting one value :). [leaves for a minute to try it].
  • DaveRandom
    DaveRandom over 12 years
    @Sebastian This could be easily re-worked to use it in /var/value format - the exact details of how it would be done depend on the situation, but in effect you are just reformatting strings from the input data, so it's not that difficult. You can just do something like foreach ($get as $key => val) $myUrl .= "/$key/$val"; (may need slight alteration depending on exactly how your URLs are formatted).
  • Sebastian
    Sebastian over 12 years
    elegant trick with the "/$key/$val"! Another solution: Yii::app()->createUrl(Yii::app()->controller->getId().'/'.Yi‌​i::app()->controller‌​->getAction()->getId‌​(), $_GET ); seems to work (after unsetting $_GET['lg'] :)!
  • GusDeCooL
    GusDeCooL over 11 years
    This should be the correct answer, since the asker want to be done with Yii
  • marcovtwout
    marcovtwout about 10 years
    This is not what the poster asked for. See the answer below: stackoverflow.com/questions/8413062/…
  • marcovtwout
    marcovtwout about 10 years
    No need to write code that Yii already provides. Read this: yiiframework.com/doc/api/1.1/CHttpRequest
  • marcovtwout
    marcovtwout about 10 years
    Hardcoding the url is a bad practise. See this topic about creating url's in general: yiiframework.com/doc/guide/1.1/en/topics.url#creating-urls
  • marcovtwout
    marcovtwout about 10 years
    Relying on one particular url style is bad and unnessecary. See this topic about creating url's in general: yiiframework.com/doc/guide/1.1/en/topics.url#creating-urls
  • Michael Butler
    Michael Butler over 9 years
    This is the wrong answer. User needs to EXCLUDE some GET parameters.
  • Mahomedalid
    Mahomedalid over 9 years
    This should be the correct answer, sometimes the url includes not just controller and action, but view, and depends on the route methods.
  • Flion
    Flion about 9 years
    Yes, great answer! (and indeed the only correct one to the question) Could use an update it for Yii2 too though..
  • Flion
    Flion about 9 years
    found it: Yii2: Yii::$app->urlManager->createUrl(array_merge([Yii::$app->req‌​uestedRoute], $getParams));
  • Vladimir
    Vladimir over 8 years
    This anwer has a lot of up votes but it is misleading, @MichaelButler answer is very good but you can see shorter yii2 specific answer stackoverflow.com/questions/8413062/…
  • RN Kushwaha
    RN Kushwaha over 7 years
    This one is the correct answer. But for yii2 it should be Yii::$app->request->pathInfo
  • Sukma Saputra
    Sukma Saputra over 7 years
    Yii2: Yii::$app->request->url
  • Alexandru Trandafir Catalin
    Alexandru Trandafir Catalin over 7 years
    I think this is wrong. Because createAbsoluteUrl expects a route not a URL. The author's original solution is quite right but a more correct one would be: $this->createUrl($this->getRoute(), $_GET) and before calling it, unset from $_GET the params that you don't wish.
  • rob006
    rob006 almost 6 years
    This is terrible answer, don't use it! It will give you a bunch of bugs when your URLs does not match routes. For example if /news/index should display route news/view for News model with slug index. With this answer you will get incorrect URL - it will redirect you to URL for /news/index route instead /news/view.
  • Nico Haase
    Nico Haase almost 6 years
    How does this remove "some" of the variables from the URL?