php 获取没有 $_GET 变量的当前 URL/URI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8413062/
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
Get current URL/URI without some of $_GET variables
提问by Sebastian
How, in Yii, to get the current page's URL. For example:
在 Yii 中如何获取当前页面的 URL。例如:
http://www.yoursite.com/your_yii_application/?lg=pl&id=15
but excluding the $GET_['lg']
(without parsing the string manually)?
但排除$GET_['lg']
(不手动解析字符串)?
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.
我的意思是,我正在寻找类似于Yii::app()->requestUrl
/Chtml::link()
方法的东西,用于返回 URL 减去一些$_GET
变量。
Edit: Current solution:
编辑:当前解决方案:
unset $_GET['lg'];
echo Yii::app()->createUrl(
Yii::app()->controller->getId().'/'.Yii::app()->controller->getAction()->getId() ,
$_GET
);
回答by Felipe Almeida
Yii 1
一
Yii::app()->request->url
For Yii2:
对于 Yii2:
Yii::$app->request->url
回答by Bhargav
Yii::app()->createAbsoluteUrl(Yii::app()->request->url)
This will output something in the following format:
这将输出以下格式的内容:
http://www.yoursite.com/your_yii_application/
回答by marcovtwout
Yii 1
一
Most of the other answers are wrong. The poster is asking for the url WITHOUT (some) $_GET-parameters.
大多数其他答案都是错误的。海报要求提供没有(一些)$_GET 参数的网址。
Here is a complete breakdown (creating url for the currently active controller, modules or not):
这是一个完整的细分(为当前活动的控制器、模块创建 url):
// without $_GET-parameters
Yii::app()->controller->createUrl(Yii::app()->controller->action->id);
// with $_GET-parameters, HAVING ONLY supplied keys
Yii::app()->controller->createUrl(Yii::app()->controller->action->id,
array_intersect_key($_GET, array_flip(['id']))); // include 'id'
// with all $_GET-parameters, EXCEPT supplied keys
Yii::app()->controller->createUrl(Yii::app()->controller->action->id,
array_diff_key($_GET, array_flip(['lg']))); // exclude 'lg'
// with ALL $_GET-parameters (as mensioned in other answers)
Yii::app()->controller->createUrl(Yii::app()->controller->action->id, $_GET);
Yii::app()->request->url;
When you don't have the same active controller, you have to specify the full path like this:
当您没有相同的活动控制器时,您必须像这样指定完整路径:
Yii::app()->createUrl('/controller/action');
Yii::app()->createUrl('/module/controller/action');
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 的 Yii 指南:http: //www.yiiframework.com/doc/guide/1.1/en/topics.url#creating-urls
回答by James Fletcher
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:
要获取绝对当前请求 url(与地址栏中看到的完全相同,使用 GET params 和 http://),我发现以下方法运行良好:
Yii::app()->request->hostInfo . Yii::app()->request->url
回答by Vladimir
In Yii2 you can do:
在 Yii2 中,您可以执行以下操作:
use yii\helpers\Url;
$withoutLg = Url::current(['lg'=>null], true);
More info: https://www.yiiframework.com/doc/api/2.0/yii-helpers-baseurl#current%28%29-detail
更多信息:https: //www.yiiframework.com/doc/api/2.0/yii-helpers-baseurl#current%28%29-detail
回答by Dinesh Patil
You are definitely searching for this
你肯定在寻找这个
Yii::app()->request->pathInfo
回答by nishanth thulasi
So, you may use
所以,你可以使用
Yii::app()->getBaseUrl(true)
to get an Absolute webroot url, and strip the http[s]://
获取绝对 webroot url,并去除 http[s]://
回答by DaveRandom
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):
我不知道在Yii中这样做,但你可能只是这样做,它应该在任何地方工作(主要来自我的回答解除这里):
// Get HTTP/HTTPS (the possible values for this vary from server to server)
$myUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && !in_array(strtolower($_SERVER['HTTPS']),array('off','no'))) ? 'https' : 'http';
// Get domain portion
$myUrl .= '://'.$_SERVER['HTTP_HOST'];
// Get path to script
$myUrl .= $_SERVER['REQUEST_URI'];
// Add path info, if any
if (!empty($_SERVER['PATH_INFO'])) $myUrl .= $_SERVER['PATH_INFO'];
$get = $_GET; // Create a copy of $_GET
unset($get['lg']); // Unset whatever you don't want
if (count($get)) { // Only add a query string if there's anything left
$myUrl .= '?'.http_build_query($get);
}
echo $myUrl;
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.
或者,您可以将 Yii 方法之一的结果传递给parse_url()
,并操纵结果以重新构建您想要的内容。
回答by Ben
Something like this should work, if run in the controller:
如果在控制器中运行,这样的事情应该可以工作:
$controller = $this;
$path = '/path/to/app/'
. $controller->module->getId() // only necessary if you're using modules
. '/' . $controller->getId()
. '/' . $controller->getAction()->getId()
. '/';
This assumes that you are using 'friendly' URLs in your app config.
这假设您在应用程序配置中使用“友好”的 URL。
回答by JAIME LAGOS
$validar= Yii::app()->request->getParam('id');