Commit 52f4006c by Qiang Xue

Fixed Url::current() implementation.

parent 34d762c2
......@@ -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.
......
......@@ -344,9 +344,28 @@ class BaseUrl
}
/**
* 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.
......@@ -354,15 +373,13 @@ class BaseUrl
* - 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);
}
}
......@@ -96,7 +96,8 @@ class UrlTest extends TestCase
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());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment