Skip to content

Commit

Permalink
Fixed Url::current() implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Jan 11, 2015
1 parent 34d762c commit 52f4006
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
17 changes: 17 additions & 0 deletions docs/guide/helper-url.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ echo Url::to('@web/images/logo.gif', true);
echo Url::to('@web/images/logo.gif', 'https');
```

Starting from version 2.0.3, you may use [[yii\helpers\Url::current()]] to create a URL based on the currently
requested route and GET parameters. You may modify or remove some of the GET parameters or add new ones by
passing a `$params` parameter to the method. For example,

```php
// assume $_GET = ['id' => 123, 'src' => 'google'], current route is "post/view"

// /index.php?r=post/view&id=123&src=google
echo Url::current();

// /index.php?r=post/view&id=123
echo Url::current(['src' => null]);
// /index.php?r=post/view&id=100&src=google
echo Url::current(['id' => 100]);
```


## Remember URLs <a name="remember-urls"></a>

There are cases when you need to remember URL and afterwards use it during processing of the one of sequential requests.
Expand Down
31 changes: 24 additions & 7 deletions framework/helpers/BaseUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,25 +344,42 @@ public static function isRelative($url)
}

/**
* Creates URL from the current one by adding parameters specified.
* Creates a URL by using the current route and the GET parameters.
*
* @param array $params associative array of parameters. If value is null, parameter will be removed.
* You may modify or remove some of the GET parameters, or add additional query parameters through
* the `$params` parameter. In particular, if you specify a parameter to be null, then this parameter
* will be removed from the existing GET parameters; all other parameters specified in `$params` will
* be merged with the existing GET parameters. For example,
*
* ```php
* // assume $_GET = ['id' => 123, 'src' => 'google'], current route is "post/view"
*
* // /index.php?r=post/view&id=123&src=google
* echo Url::current();
*
* // /index.php?r=post/view&id=123
* echo Url::current(['src' => null]);
*
* // /index.php?r=post/view&id=100&src=google
* echo Url::current(['id' => 100]);
* ```
*
* @param array $params an associative array of parameters that will be merged with the current GET parameters.
* If a parameter value is null, the corresponding GET parameter will be removed.
* @param boolean|string $scheme the URI scheme to use in the generated URL:
*
* - `false` (default): generating a relative URL.
* - `true`: returning an absolute base URL whose scheme is the same as that in [[\yii\web\UrlManager::hostInfo]].
* - string: generating an absolute URL with the specified scheme (either `http` or `https`).
*
* @return string the generated URL
*
* @since 2.0.2
*/
public static function current(array $params = [], $scheme = false)
{
$currentParms = Yii::$app->controller->actionParams;
$currentParms[0] = Yii::$app->controller->getRoute();
$route = ArrayHelper::merge($currentParms, $params);

$currentParams = Yii::$app->getRequest()->getQueryParams();
$currentParams[0] = Yii::$app->controller->getRoute();
$route = ArrayHelper::merge($currentParams, $params);
return static::toRoute($route, $scheme);
}
}
3 changes: 2 additions & 1 deletion tests/unit/framework/helpers/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public function testToRoute()

public function testCurrent()
{
$this->mockAction('page', 'view', null, ['id' => 10, 'name' => 'test']);
$this->mockAction('page', 'view', null, []);
\Yii::$app->request->setQueryParams(['id' => 10, 'name' => 'test']);

$this->assertEquals('/base/index.php?r=page%2Fview&id=10&name=test', Url::current());

Expand Down

0 comments on commit 52f4006

Please sign in to comment.