简体   繁体   中英

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

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.

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.

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()->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

The best and shorter way I found is:

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

OR

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

For Yii2, use \\yii\\helpers\\Url::canonical() .

Documentation: https://www.yiiframework.com/doc/api/2.0/yii-helpers-baseurl#canonical()-detail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM