How do I obtain the current url without params in Yii?
Solution 1
For:
full URL (
http://localhost/even/page/id/2/?t=3
) useYii::app()->request->getUrl()
,URL without parameters (
http://localhost/even/page/id/2/
useYii::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());
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, 2022Comments
-
Itay Moav -Malimovka almost 2 years
I am using Yii.
if my url ishttp://localhost/evengee/event/page/id/2/sfn+master/?tab=3
My real url (file path) is onlyhttp://localhost/evengee
How would I obtain, preferably in the view:- full url
http://localhost/evengee/event/page/id/2/sfn+master/?tab=3
- 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.
- full url
-
GusDeCooL over 11 yearsi try
Yii::app()->request->getUrl()
, but this didn't return domain name. -
111 almost 11 yearsCHttpRequest and its behaviors do not have a method or closure named "getRequestUrl". that should be an 'i' not an 'l'
-
Jesse C about 10 yearsUse createAbsoluteUrl if you want the full domain name.
$this->createAbsoluteUrl( Yii::app()->request->getUrl() );
ORYii::app()->createAbsoluteUrl( Yii::app()->request->getUrl() );
-
Jeff_Alieffson about 9 yearsIt should be Yii::app()->request->getUrl();
-
Itay Moav -Malimovka over 2 yearsIS this FW still being developed? Question was from 10 years ago
-
flaviovs over 2 yearsYes it is. Yii2 is the current version. Yii3 is in active development. yiiframework.com