php-获取当前的URL / URI,但不包含$ _GET变量
在Yii中,如何获取当前页面的URL。 例如:
http://www.yoursite.com/your_yii_application/?lg=pl&id=15
但不包括$GET_['lg'](无需手动解析字符串)?
我的意思是,我正在寻找类似于Yii::app()->requestUrl/Chtml::link()方法的方法,用于返回减去$_GET变量中的一部分的URL。
编辑:当前解决方案:
unset $_GET['lg'];
echo Yii::app()->createUrl(
Yii::app()->controller->getId().'/'.Yii::app()->controller->getAction()->getId() ,
$_GET
);
15个解决方案
73 votes
这个1
Yii::app()->request->url
对于Yii2:
Yii::$app->request->url
Felipe Almeida answered 2020-08-07T06:46:53Z
30 votes
Yii::app()->createAbsoluteUrl(Yii::app()->request->url)
这将以以下格式输出:
http://www.yoursite.com/your_yii_application/
Bhargav answered 2020-08-07T06:47:13Z
21 votes
这个1
其他大多数答案都是错误的。 张贴者要求输入没有(某些)$ _ GET参数的URL。
这是一个完整的细分(是否为当前活动的控制器,模块创建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;
如果没有相同的活动控制器,则必须指定完整路径,如下所示:
Yii::app()->createUrl('/controller/action');
Yii::app()->createUrl('/module/controller/action');
总体上查看有关构建网址的Yii指南:[http://www.yiiframework.com/doc/guide/1.1/en/topics.url#creating-urls]
marcovtwout answered 2020-08-07T06:47:51Z
12 votes
要获取绝对的当前请求url(正是在地址栏中看到的,带有GET参数和[http://]),我发现以下方法很有效:
Yii::app()->request->hostInfo . Yii::app()->request->url
James Fletcher answered 2020-08-07T06:48:12Z
7 votes
在Yii2中,您可以执行以下操作:
use yii\helpers\Url;
$withoutLg = Url::current(['lg'=>null], true);
更多信息:[https://www.yiiframework.com/doc/api/2.0/yii-helpers-baseurl#current%28%29-detail]
Vladimir answered 2020-08-07T06:48:38Z
5 votes
我不知道如何在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;
或者,您可以将Yii方法之一的结果传递给parse_url(),然后操纵结果以重新构建所需的结果。
DaveRandom answered 2020-08-07T06:49:03Z
5 votes
因此,您可以使用
Yii::app()->getBaseUrl(true)
获取绝对的webroot网址,并删除[http [s]://]
nishanth thulasi answered 2020-08-07T06:49:28Z
4 votes
您肯定是在寻找这个
Yii::app()->request->pathInfo
Dinesh Patil answered 2020-08-07T06:49:48Z
1 votes
如果在控制器中运行,类似这样的东西应该可以工作:
$controller = $this;
$path = '/path/to/app/'
. $controller->module->getId() // only necessary if you're using modules
. '/' . $controller->getId()
. '/' . $controller->getAction()->getId()
. '/';
假设您在应用程序配置中使用“友好” URL。
Ben answered 2020-08-07T06:50:12Z
1 votes
Yii2
Url::current([], true);
要么
Url::current();
Ganesh Patel answered 2020-08-07T06:50:36Z
0 votes
$validar= Yii::app()->request->getParam('id');
JAIME LAGOS answered 2020-08-07T06:50:52Z
0 votes
对于Yii2:这应该比Yii::$app->request->url更安全Yii::$app->request->absoluteUrl
Syakur Rahman answered 2020-08-07T06:51:12Z
-1 votes
尝试使用此变体:
<?php echo Yii::app()->createAbsoluteUrl('your_yii_application/?lg=pl', array('id'=>$model->id));?>
我猜这是最简单的方法。
answered 2020-08-07T06:51:36Z
-1 votes
大多数答案是错误的。
问题是要获取没有某些查询参数的url。
这是起作用的功能。 实际上,它还能做更多的事情。 您可以删除不需要的参数,也可以添加或修改现有参数。
/**
* Function merges the query string values with the given array and returns the new URL
* @param string $route
* @param array $mergeQueryVars
* @param array $removeQueryVars
* @return string
*/
public static function getUpdatedUrl($route = '', $mergeQueryVars = [], $removeQueryVars = [])
{
$currentParams = $request = Yii::$app->request->getQueryParams();
foreach($mergeQueryVars as $key=> $value)
{
$currentParams[$key] = $value;
}
foreach($removeQueryVars as $queryVar)
{
unset($currentParams[$queryVar]);
}
$currentParams[0] = $route == '' ? Yii::$app->controller->getRoute() : $route;
return Yii::$app->urlManager->createUrl($currentParams);
}
用法:
ClassName:: getUpdatedUrl('',[],['remove_this1','remove_this2'])
这将从网址中删除查询参数“ remove_this1”和“ remove_this2”,并返回新的网址
Codeformer answered 2020-08-07T06:52:14Z
-1 votes
echo Yii::$app->request->url;
nageen nayak answered 2020-08-07T06:52:29Z