获取当前 URL/URI,不带某些 $_GET 变量
在 Yii 中如何获取当前页面的 URL。例如:
http://www.yoursite.com/your_yii_application/?lg=pl&id=15
但排除 $GET_['lg']
(不手动解析字符串)?
我的意思是,我正在寻找类似于 Yii::app()->requestUrl
/ Chtml::link()
方法,用于返回 URL 减去一些$_GET
变量。
编辑:当前解决方案:
unset $_GET['lg'];
echo Yii::app()->createUrl(
Yii::app()->controller->getId().'/'.Yii::app()->controller->getAction()->getId() ,
$_GET
);
How, in Yii, to get the current page's URL. For example:
http://www.yoursite.com/your_yii_application/?lg=pl&id=15
but excluding the $GET_['lg']
(without parsing the string manually)?
I mean, I'm looking for something similar to the Yii::app()->requestUrl
/ Chtml::link()
methods, for returning URLs minus some of the $_GET
variables.
Edit: Current solution:
unset $_GET['lg'];
echo Yii::app()->createUrl(
Yii::app()->controller->getId().'/'.Yii::app()->controller->getAction()->getId() ,
$_GET
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
Yii 1
对于 Yii2:
Yii 1
For Yii2:
这将输出以下格式的内容:
This will output something in the following format:
Yii 1
大多数其他答案都是错误的。发帖者要求提供没有(某些)$_GET 参数的 url。
这是一个完整的细分(为当前活动控制器、模块或不创建 url):
当您没有相同的活动控制器时,您必须指定完整路径,如下所示:
查看 Yii 构建 url 指南: http://www.yiiframework.com/doc/guide/1.1/en/topics.url#creating-urls
Yii 1
Most of the other answers are wrong. The poster is asking for the url WITHOUT (some) $_GET-parameters.
Here is a complete breakdown (creating url for the currently active controller, modules or not):
When you don't have the same active controller, you have to specify the full path like this:
Check out the Yii guide for building url's in general: http://www.yiiframework.com/doc/guide/1.1/en/topics.url#creating-urls
为了获取当前的绝对请求 url(与地址栏中所见的完全相同,使用 GET params 和 http://),我发现以下方法效果很好:
To get the absolute current request url (exactly as seen in the address bar, with GET params and http://) I found that the following works well:
在 Yii2 中你可以这样做:
更多信息:
https://www.yiiframework.com /doc/api/2.0/yii-helpers-baseurl#current%28%29-detail
In Yii2 you can do:
More info:
https://www.yiiframework.com/doc/api/2.0/yii-helpers-baseurl#current%28%29-detail
我不知道如何在 Yii 中执行此操作,但您可以执行此操作,并且它应该在任何地方都可以工作(很大程度上来自我的答案 此处):
或者,您可以将 Yii 方法之一的结果传递到
parse_url()
,并操作结果重建你想要的东西。I don't know about doing it in Yii, but you could just do this, and it should work anywhere (largely lifted from my answer here):
Alternatively, you could pass the result of one of the Yii methods into
parse_url()
, and manipulate the result to re-build what you want.您肯定正在寻找这个
You are definitely searching for this
因此,您可以用来
获取绝对 webroot url,并去掉 http[s]://
So, you may use
to get an Absolute webroot url, and strip the http[s]://
如果在控制器中运行,类似这样的东西应该可以工作:
这假设您在应用程序配置中使用“友好”URL。
Something like this should work, if run in the controller:
This assumes that you are using 'friendly' URLs in your app config.
Yii2
或
Yii2
or
对于 Yii2:
这应该比
Yii::$app->request->absoluteUrl
更安全,而不是Yii::$app->request->url
For Yii2:
This should be safer
Yii::$app->request->absoluteUrl
rather thanYii::$app->request->url
对于 Yii1
我发现这是一种简洁的方法,首先从 CUrlManager 获取当前路由,然后再次使用该路由来构建新的 url。这样您就不会“看到”应用程序的 baseUrl,请参阅下面的示例。
带有控制器/操作的示例:
带有模块/控制器/操作的示例:
仅当您的 url 完全被 CUrlManager 规则覆盖时才有效:)
For Yii1
I find it a clean way to first get the current route from the CUrlManager, and then use that route again to build the new url. This way you don't 'see' the baseUrl of the app, see the examples below.
Example with a controller/action:
Example with a module/controller/action:
This works only if your urls are covered perfectly by the rules of CUrlManager :)
对于 Yii2,
我感到相当惊讶的是 Yii2 的正确答案没有出现在任何响应中。
我使用的答案是:
你也可以使用:
...但我认为第一个版本更明显地表明你的意图是使用当前请求路由生成一个 url。 (即“/index.php?admin%2Findex”,如果您碰巧从 AdminController 中的 actionIndex() 运行。
您可以通过传递 true 作为第二个参数来获取具有模式和域的绝对路由,因此:
.. .会返回类似“https://your.site.domain/index.php?admin%2Findex”的内容。
For Yii2
I was rather surprised that the correct answer for Yii2 was in none of the responses.
The answer I use is:
You can also use:
...but I think the first version makes it more obvious that your intention is to generate a url with the curent request route. (i.e. "/index.php?admin%2Findex" if you happened to be running from the actionIndex() in the AdminController.
You can get an absolute route with the schema and domain by passing true as a second parameter, so:
...would return something like "https://your.site.domain/index.php?admin%2Findex" instead.
尝试使用这个变体:
我想这是最简单的方法。
Try to use this variant:
It is the easiest way, I guess.
大多数答案都是错误的。
问题是获取没有某些查询参数的 url 。
这是有效的功能。它实际上做了更多的事情。您可以删除不需要的参数,也可以添加或修改现有参数。
用法:
这将从 URL 中删除查询参数“remove_this1”和“remove_this2”并返回新的 URL
Most of the answers are wrong.
The Question is to get url without some query param .
Here is the function that works. It does more things actually. You can remove the param that you don't want and you can add or modify an existing one.
usage:
This will remove query params 'remove_this1' and 'remove_this2' from URL and return you the new URL