php 如何在 Yii 中获取没有参数的当前 url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8621876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I obtain the current url without params in Yii?
提问by Itay Moav -Malimovka
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:
我正在使用 Yii。
如果我的网址是http://localhost/evengee/event/page/id/2/sfn+master/?tab=3
我的真实网址(文件路径),http://localhost/evengee
我将如何获取,最好在视图中:
- 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/
- 完整网址
http://localhost/evengee/event/page/id/2/sfn+master/?tab=3
- 没有显式参数的 url
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.
我知道我可以通过 ? 并使用 $_SERVER。更愿意使用 Yii 本地方法。
回答by Itay Moav -Malimovka
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()
.
完整的 URL (
http://localhost/even/page/id/2/?t=3
) 使用Yii::app()->request->getUrl()
,不带参数的 URL(
http://localhost/even/page/id/2/
使用Yii::app()->request->getPathInfo()
.
Second one will give you the URL without schema, server and query string. This seems good enough.
第二个将为您提供没有架构、服务器和查询字符串的 URL。这似乎已经足够好了。
回答by Ben
To get the full URL, use the getRequestUrl()
method of CHttpRequest
Yii::app()->request->getRequestUrl();
要获取完整的 URL,请使用以下getRequestUrl()
方法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
您可以从 CApplication 方法中获取当前页面的控制器、模块和操作名称
Yii::app()->getController()->id; //gets you the controller id
Yii::app()->getController()->getAction()->id; //gets you the action id
Yii::app()->getController()->getAction()->id; //gets you the action id
You can piece together a URL using the baseURL property of CApplication
您可以使用 CApplication 的 baseURL 属性拼凑一个 URL
Yii::app()->baseURL
Yii::app()->baseURL
回答by Neeraj
The best and shorter way I found is:
我发现的最好和更短的方法是:
Yii::app()->createAbsoluteUrl(Yii::app()->request->getPathInfo());
Yii::app()->createAbsoluteUrl(Yii::app()->request->getPathInfo());
OR
或者
$this->createAbsoluteUrl(Yii::app()->request->getPathInfo());
$this->createAbsoluteUrl(Yii::app()->request->getPathInfo());