![](/img/trans.png)
[英]How do i get Module, Controller, action and request params of a Yii based url without using Yii
[英]How do I obtain the current url without params in Yii?
我正在使用 Yii。
如果我的網址是http://localhost/evengee/event/page/id/2/sfn+master/?tab=3
我的真實網址(文件路徑)只是http://localhost/evengee
我將如何獲得,最好在視圖中:
http://localhost/evengee/event/page/id/2/sfn+master/?tab=3
http://localhost/evengee/event/page/id/2/sfn+master/
我知道我可以通過 ? 並使用 $_SERVER。 更願意使用 Yii 本地方法。
對於:
完整網址( http://localhost/even/page/id/2/?t=3
)使用Yii::app()->request->getUrl()
,
不帶參數的URL( http://localhost/even/page/id/2/
使用Yii::app()->request->getPathInfo()
。
第二個將為您提供不帶模式,服務器和查詢字符串的URL。 這似乎足夠好。
要獲得完整的URL,使用getRequestUrl()
的方法CHttpRequest
Yii::app()->request->getRequestUrl();
您可以從CApplication方法Yii::app()->getController()->id; //gets you the controller id
獲取當前頁面的控制器,模塊和動作名稱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
您可以使用CApplication的baseURL屬性拼湊一個URL
Yii::app()->baseURL
我發現的最佳和較短的方法是:
Yii::app()->createAbsoluteUrl(Yii::app()->request->getPathInfo());
要么
$this->createAbsoluteUrl(Yii::app()->request->getPathInfo());
對於 Yii2,使用\\yii\\helpers\\Url::canonical()
yii \\yii\\helpers\\Url::canonical()
。
文檔: https : //www.yiiframework.com/doc/api/2.0/yii-helpers-baseurl#canonical()-detail
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.