获取当前 URL/URI,而不带一些 $_GET 变量


Get current URL/URI without some of $_GET variables

在 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 
);

Yii 1

Yii::app()->request->url

对于 Yii2:

Yii::$app->request->url
Yii::app()->createAbsoluteUrl(Yii::app()->request->url)

这将以以下格式输出一些内容:

http://www.yoursite.com/your_yii_application/

Yii 1

大多数其他答案都是错误的。海报要求没有(一些($_GET参数的网址。

这是一个完整的细分(为当前活动控制器创建 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 指南,了解一般构建 url 的内容:http://www.yiiframework.com/doc/guide/1.1/en/topics.url#creating-urls

要获取绝对当前请求 url(与地址栏中所示,使用 GET 参数和 http://(,我发现以下内容效果很好:

Yii::app()->request->hostInfo . Yii::app()->request->url

在 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

我不知道

在 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() 中,并操纵结果来重建你想要的东西。

你肯定在搜索这个

Yii::app()->request->pathInfo

所以,你可以使用

Yii::app()->getBaseUrl(true)

获取一个绝对的 webroot 网址,并去除 http[s]://

如果在控制器中运行,这样的事情应该可以工作:

$controller = $this;
$path = '/path/to/app/' 
  . $controller->module->getId() // only necessary if you're using modules
  . '/' . $controller->getId() 
  . '/' . $controller->getAction()->getId()
. '/';

这假定您在应用配置中使用"友好"URL。

$validar= Yii::app()->request->getParam('id');

Yii2

Url::current([], true);

Url::current();

对于 Yii2:这应该更安全Yii::$app->request->absoluteUrl而不是Yii::$app->request->url

For Yii1

我发现这是一种干净的方法,首先从 CUrlManager 获取当前路由,然后再次使用该路由来构建新 url。这样,您就不会"看到"应用程序的 baseUrl,请参阅以下示例。

控制器/操作示例:

GET /app/customer/index/?random=param
$route = Yii::app()->urlManager->parseUrl(Yii::app()->request);
var_dump($route); // string 'customer/index' (length=14)
$new = Yii::app()->urlManager->createUrl($route, array('new' => 'param'));
var_dump($new); // string '/app/customer/index?new=param' (length=29)

模块/控制器/操作的示例:

GET /app/order/product/admin/?random=param
$route = Yii::app()->urlManager->parseUrl(Yii::app()->request);
var_dump($route); // string 'order/product/admin' (length=19)
$new = Yii::app()->urlManager->createUrl($route, array('new' => 'param'));
var_dump($new); string '/app/order/product/admin?new=param' (length=34)

仅当您的网址被 CUrlManager 的规则完美覆盖时,这才有效:)

For Yii2

令我相当惊讶的是,Yii2 的正确答案没有出现在任何答案中。

我使用的答案是:

Url::to(['']),

您还可以使用:

Url::to()

。但我认为第一个版本更清楚地表明您的意图是生成一个带有当前请求路由的 URL。(即"/index.php?admin%2Findex",如果你碰巧从 AdminController 中的 actionIndex(( 运行。

您可以通过传递 true 作为第二个参数来获取具有架构和域的绝对路由,因此:

Url::to([''], true)

。将返回类似"https://your.site.domain/index.php?admin%2Findex"的内容。

尝试使用此变体:

<?php echo Yii::app()->createAbsoluteUrl('your_yii_application/?lg=pl', array('id'=>$model->id));?>

我想这是最简单的方法。

大多数答案都是错误的。

问题是在没有一些查询参数的情况下获取网址。

这是有效的功能。它实际上做了更多的事情。您可以删除不需要的参数,也可以添加或修改现有参数。

/**
 * 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'])

这将从 URL 中删除查询参数"remove_this1"和"remove_this2",并返回新 URL

echo Yii::$app->request->url;