How do I obtain the current url without params in Yii?

30,878

Solution 1

For:

  • full URL (http://localhost/even/page/id/2/?t=3) use Yii::app()->request->getUrl(),

  • URL without parameters (http://localhost/even/page/id/2/ use Yii::app()->request->getPathInfo().

Second one will give you the URL without schema, server and query string. This seems good enough.

Solution 2

To get the full URL, use the getRequestUrl() method of CHttpRequest Yii::app()->request->getRequestUrl();

You can get the controller, module and action name of the current page from the CApplication methods Yii::app()->getController()->id; //gets you the controller id

Yii::app()->getController()->getAction()->id; //gets you the action id

You can piece together a URL using the baseURL property of CApplication

Yii::app()->baseURL

Solution 3

The best and shorter way I found is:

Yii::app()->createAbsoluteUrl(Yii::app()->request->getPathInfo());

OR

$this->createAbsoluteUrl(Yii::app()->request->getPathInfo());

Share:
30,878
Itay Moav -Malimovka
Author by

Itay Moav -Malimovka

SOreadytohelp Below are some of the open source projects I work on. A PHP Library the wrappes SurveyMonkey's API https://github.com/itay-moav/TheKofClient A tool to Schema Check, manage Stored Procedures, Triggers, Views and get autocompletion: https://github.com/itay-moav/rahl_commander A fun way to point users at the right direction in your site ;-) https://github.com/itay-moav/babahandofgod An old version of WMD which I converted to Mootools, 8 years ago... http://moowmd.awardspace.info Feel free to contact me through linkedin http://www.linkedin.com/in/itaymoav

Updated on July 05, 2022

Comments

  • Itay Moav -Malimovka
    Itay Moav -Malimovka almost 2 years

    I am using Yii.
    if my url is http://localhost/evengee/event/page/id/2/sfn+master/?tab=3 My real url (file path) is only http://localhost/evengee
    How would I obtain, preferably in the view:

    1. full url http://localhost/evengee/event/page/id/2/sfn+master/?tab=3
    2. url without explicit parameters http://localhost/evengee/event/page/id/2/sfn+master/

    I know I can split/explode str_replace by the ? and use $_SERVER. Would prefer to use Yii native methods.

  • GusDeCooL
    GusDeCooL over 11 years
    i try Yii::app()->request->getUrl(), but this didn't return domain name.
  • 111
    111 almost 11 years
    CHttpRequest and its behaviors do not have a method or closure named "getRequestUrl". that should be an 'i' not an 'l'
  • Jesse C
    Jesse C about 10 years
    Use createAbsoluteUrl if you want the full domain name. $this->createAbsoluteUrl( Yii::app()->request->getUrl() ); OR Yii::app()->createAbsoluteUrl( Yii::app()->request->getUrl() );
  • Jeff_Alieffson
    Jeff_Alieffson about 9 years
    It should be Yii::app()->request->getUrl();
  • Itay Moav -Malimovka
    Itay Moav -Malimovka over 2 years
    IS this FW still being developed? Question was from 10 years ago
  • flaviovs
    flaviovs over 2 years
    Yes it is. Yii2 is the current version. Yii3 is in active development. yiiframework.com