简体   繁体   English

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

[英]Get current URL/URI without some of $_GET variables

How, in Yii, to get the current page's URL.如何在 Yii 中获取当前页面的 URL。 For example:例如:

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

Yii 1 Yii 1

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

For Yii2: 对于Yii2:

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

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

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

Yii 1 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 查看Yii关于构建网址的指南: http//www.yiiframework.com/doc/guide/1.1/en/topics.url#creating-urls

要获得绝对当前请求URL(完全如地址栏中所示,使用GET参数和http://),我发现以下情况很有效:

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

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

So, you may use 所以,你可以使用

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

to get an Absolute webroot url, and strip the http[s]:// 获取一个绝对的webroot网址,并删除http [s]://

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() ,并操纵结果以重新构建您想要的内容。

你肯定在寻找这个

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

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. 这假设您在应用配置中使用“友好”网址。

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

Yii2 Yii2

Url::current([], true);

or 要么

Url::current();

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

For Yii1对于 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.我发现首先从 CUrlManager 获取当前路由,然后再次使用该路由构建新 url 是一种干净的方法。 This way you don't 'see' the baseUrl of the app, see the examples below.这样您就不会“看到”应用程序的 baseUrl,请参阅下面的示例。

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:)仅当您的网址完全符合 CUrlManager 的规则时,这才有效:)

For Yii2对于 Yii2

I was rather surprised that the correct answer for Yii2 was in none of the responses.我很惊讶 Yii2 的正确答案在所有回复中都没有。

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. ...但我认为第一个版本更明显地表明您的意图是使用当前请求路由生成一个 url。 (ie "/index.php?admin%2Findex" if you happened to be running from the actionIndex() in the AdminController. (即“/index.php?admin%2Findex”,如果您恰好从 AdminController 中的 actionIndex() 运行。

You can get an absolute route with the schema and domain by passing true as a second parameter, so:您可以通过将 true 作为第二个参数传递来获得具有架构和域的绝对路由,因此:

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

...would return something like "https://your.site.domain/index.php?admin%2Findex" instead. ...会返回类似“https://your.site.domain/index.php?admin%2Findex”的内容。

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. 我想这是最简单的方法。

Most of the answers are wrong. 大多数答案都是错误的。

The Question is to get url without some query param . 问题是获取没有查询参数的url。

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 这将从URL中删除查询参数'remove_this1'和'remove_this2',并返回新的URL

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM