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

发布于 2024-12-20 08:05:52 字数 525 浏览 7 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

等风也等你 2024-12-27 08:05:52

Yii 1

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

对于 Yii2:

Yii::$app->request->url

Yii 1

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

For Yii2:

Yii::$app->request->url
最偏执的依靠 2024-12-27 08:05:52
Yii::app()->createAbsoluteUrl(Yii::app()->request->url)

这将输出以下格式的内容:

http://www.yoursite.com/your_yii_application/
Yii::app()->createAbsoluteUrl(Yii::app()->request->url)

This will output something in the following format:

http://www.yoursite.com/your_yii_application/
薄荷港 2024-12-27 08:05:52

Yii 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 构建 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):

// 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

独闯女儿国 2024-12-27 08:05:52

为了获取当前的绝对请求 url(与地址栏中所见的完全相同,使用 GET params 和 http://),我发现以下方法效果很好:

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

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:

Yii::app()->request->hostInfo . Yii::app()->request->url
救星 2024-12-27 08:05:52

在 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

In Yii2 you can do:

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

迟到的我 2024-12-27 08:05:52

我不知道如何在 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(),并操作结果重建你想要的东西。

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):

// 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.

沫离伤花 2024-12-27 08:05:52

您肯定正在寻找这个

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

You are definitely searching for this

Yii::app()->request->pathInfo
浮华 2024-12-27 08:05:52

因此,您可以用来

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

获取绝对 webroot url,并去掉 http[s]://

So, you may use

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

to get an Absolute webroot url, and strip the http[s]://

芸娘子的小脾气 2024-12-27 08:05:52

如果在控制器中运行,类似这样的东西应该可以工作:

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

这假设您在应用程序配置中使用“友好”URL。

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.

々眼睛长脚气 2024-12-27 08:05:52
$validar= Yii::app()->request->getParam('id');
$validar= Yii::app()->request->getParam('id');
半岛未凉 2024-12-27 08:05:52

Yii2

Url::current([], true);

Url::current();

Yii2

Url::current([], true);

or

Url::current();
梦里南柯 2024-12-27 08:05:52

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

For Yii2:
This should be safer Yii::$app->request->absoluteUrl rather than Yii::$app->request->url

恰似旧人归 2024-12-27 08:05:52

对于 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)

仅当您的 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:

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)

Example with a module/controller/action:

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)

This works only if your urls are covered perfectly by the rules of CUrlManager :)

清风疏影 2024-12-27 08:05:52

对于 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”的内容。

For Yii2

I was rather surprised that the correct answer for Yii2 was in none of the responses.

The answer I use is:

Url::to(['']),

You can also use:

Url::to()

...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:

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

...would return something like "https://your.site.domain/index.php?admin%2Findex" instead.

梦幻的心爱 2024-12-27 08:05:52

尝试使用这个变体:

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

我想这是最简单的方法。

Try to use this variant:

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

It is the easiest way, I guess.

躲猫猫 2024-12-27 08:05:52

大多数答案都是错误的。

问题是获取没有某些查询参数的 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'])

这将从 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.

/**
 * 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);

}

usage:

ClassName:: getUpdatedUrl('',[],['remove_this1','remove_this2'])

This will remove query params 'remove_this1' and 'remove_this2' from URL and return you the new URL

雅心素梦 2024-12-27 08:05:52
echo Yii::$app->request->url;
echo Yii::$app->request->url;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文