Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
52f4006c
Commit
52f4006c
authored
Jan 11, 2015
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed Url::current() implementation.
parent
34d762c2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
8 deletions
+43
-8
helper-url.md
docs/guide/helper-url.md
+17
-0
BaseUrl.php
framework/helpers/BaseUrl.php
+24
-7
UrlTest.php
tests/unit/framework/helpers/UrlTest.php
+2
-1
No files found.
docs/guide/helper-url.md
View file @
52f4006c
...
...
@@ -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.
...
...
framework/helpers/BaseUrl.php
View file @
52f4006c
...
...
@@ -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
);
}
}
tests/unit/framework/helpers/UrlTest.php
View file @
52f4006c
...
...
@@ -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
());
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment