如何在Yii中获取没有参数的当前URL?

Ita*_*vka 12 php url url-routing yii

我正在使用Yii.
如果我的网址是http://localhost/evengee/event/page/id/2/sfn+master/?tab=3 我的真实网址(文件路径)只是http://localhost/evengee
我将如何获得,最好是在视图中:

  1. 完整的网址 http://localhost/evengee/event/page/id/2/sfn+master/?tab=3
  2. 没有显式参数的url http://localhost/evengee/event/page/id/2/sfn+master/

我知道我可以拆分/爆炸str_replace吗?并使用$ _SERVER.宁愿使用Yii本机方法.

Ita*_*vka 18

对于:

  • 完整的URL(http://localhost/even/page/id/2/?t=3)使用 Yii::app()->request->getUrl(),

  • 没有参数的URL(http://localhost/even/page/id/2/使用 Yii::app()->request->getPathInfo().

第二个将为您提供没有架构,服务器和查询字符串的URL.这似乎足够好了.

  • 我尝试`Yii :: app() - > request-> getUrl()`,但这并没有返回域名. (4认同)

Ben*_*Ben 5

要获取完整的 URL,请使用getRequestUrl()以下方法CHttpRequest Yii::app()->request->getRequestUrl();

您可以从 CApplication 方法获取当前页面的控制器、模块和操作名称 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

  • CHttpRequest 及其行为没有名为“getRequestUrl”的方法或闭包。那应该是“i”而不是“l” (4认同)