Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 360
Itay Moav -Malimovka Avatar asked Dec 24 '11 00:12

Itay Moav -Malimovka


People also ask

How to get URL in Yii2?

To get the base URL of the current request use the following: $relativeBaseUrl = Url::base(); $absoluteBaseUrl = Url::base(true); $httpsAbsoluteBaseUrl = Url::base('https'); The only parameter of the method works exactly the same as for Url::home() .

How to get value from URL in Yii2?

Yii::app()->request->getUrl(); Yii::app()->request->getParams('id'); $_GET['id'];

How to create URL in Yii?

For example, you can use the following code to create a URL for the post/view action: use yii\helpers\Url; // Url::to() calls UrlManager::createUrl() to create a URL $url = Url::to(['post/view', 'id' => 100]);


1 Answers

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.

like image 177
Itay Moav -Malimovka Avatar answered Oct 06 '22 06:10

Itay Moav -Malimovka