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
a5fe4a6a
Commit
a5fe4a6a
authored
Feb 20, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed URLManager bugs.
parent
448fa79b
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
20 additions
and
9 deletions
+20
-9
UrlManager.php
framework/web/UrlManager.php
+2
-2
UrlRule.php
framework/web/UrlRule.php
+7
-0
bootstrap.php
tests/unit/bootstrap.php
+0
-0
UrlManagerTest.php
tests/unit/framework/web/UrlManagerTest.php
+11
-7
No files found.
framework/web/UrlManager.php
View file @
a5fe4a6a
...
@@ -156,7 +156,7 @@ class UrlManager extends Component
...
@@ -156,7 +156,7 @@ class UrlManager extends Component
public
function
createUrl
(
$route
,
$params
=
array
())
public
function
createUrl
(
$route
,
$params
=
array
())
{
{
$anchor
=
isset
(
$params
[
'#'
])
?
'#'
.
$params
[
'#'
]
:
''
;
$anchor
=
isset
(
$params
[
'#'
])
?
'#'
.
$params
[
'#'
]
:
''
;
unset
(
$
anchor
[
'#'
]);
unset
(
$
params
[
'#'
]);
$route
=
trim
(
$route
,
'/'
);
$route
=
trim
(
$route
,
'/'
);
$baseUrl
=
$this
->
getBaseUrl
();
$baseUrl
=
$this
->
getBaseUrl
();
...
@@ -165,7 +165,7 @@ class UrlManager extends Component
...
@@ -165,7 +165,7 @@ class UrlManager extends Component
/** @var $rule UrlRule */
/** @var $rule UrlRule */
foreach
(
$this
->
rules
as
$rule
)
{
foreach
(
$this
->
rules
as
$rule
)
{
if
((
$url
=
$rule
->
createUrl
(
$this
,
$route
,
$params
))
!==
false
)
{
if
((
$url
=
$rule
->
createUrl
(
$this
,
$route
,
$params
))
!==
false
)
{
return
rtrim
(
$baseUrl
,
'/'
)
.
$url
.
$anchor
;
return
rtrim
(
$baseUrl
,
'/'
)
.
'/'
.
$url
.
$anchor
;
}
}
}
}
...
...
framework/web/UrlRule.php
View file @
a5fe4a6a
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
namespace
yii\web
;
namespace
yii\web
;
use
yii\base\Object
;
use
yii\base\Object
;
use
yii\base\InvalidConfigException
;
/**
/**
* UrlRule represents a rule used for parsing and generating URLs.
* UrlRule represents a rule used for parsing and generating URLs.
...
@@ -86,6 +87,12 @@ class UrlRule extends Object
...
@@ -86,6 +87,12 @@ class UrlRule extends Object
*/
*/
public
function
init
()
public
function
init
()
{
{
if
(
$this
->
pattern
===
null
)
{
throw
new
InvalidConfigException
(
'UrlRule::pattern must be set.'
);
}
if
(
$this
->
route
===
null
)
{
throw
new
InvalidConfigException
(
'UrlRule::route must be set.'
);
}
if
(
$this
->
verb
!==
null
)
{
if
(
$this
->
verb
!==
null
)
{
if
(
is_array
(
$this
->
verb
))
{
if
(
is_array
(
$this
->
verb
))
{
foreach
(
$this
->
verb
as
$i
=>
$verb
)
{
foreach
(
$this
->
verb
as
$i
=>
$verb
)
{
...
...
tests/unit/bootstrap.php
View file @
a5fe4a6a
tests/unit/framework/web/UrlManagerTest.php
View file @
a5fe4a6a
<?php
<?php
namespace
yiiunit\framework\web
;
namespace
yiiunit\framework\web
;
use
yii\web\Application
;
use
yii\web\UrlManager
;
use
yii\web\UrlManager
;
class
UrlManagerTest
extends
\yiiunit\TestCase
class
UrlManagerTest
extends
\yiiunit\TestCase
{
{
public
function
testCreateUrl
()
public
function
testCreateUrl
()
{
{
new
Application
(
'test'
,
__DIR__
.
'/../..'
);
// default setting with '/' as base url
// default setting with '/' as base url
$manager
=
new
UrlManager
(
array
(
$manager
=
new
UrlManager
(
array
(
'baseUrl'
=>
'/'
,
'baseUrl'
=>
'/'
,
...
@@ -14,14 +16,14 @@ class UrlManagerTest extends \yiiunit\TestCase
...
@@ -14,14 +16,14 @@ class UrlManagerTest extends \yiiunit\TestCase
$url
=
$manager
->
createUrl
(
'post/view'
);
$url
=
$manager
->
createUrl
(
'post/view'
);
$this
->
assertEquals
(
'/?r=post/view'
,
$url
);
$this
->
assertEquals
(
'/?r=post/view'
,
$url
);
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$this
->
assertEquals
(
'/?r=post/view&id=1&title=sample
%20
post'
,
$url
);
$this
->
assertEquals
(
'/?r=post/view&id=1&title=sample
+
post'
,
$url
);
// default setting with '/test/' as base url
// default setting with '/test/' as base url
$manager
=
new
UrlManager
(
array
(
$manager
=
new
UrlManager
(
array
(
'baseUrl'
=>
'/test/'
,
'baseUrl'
=>
'/test/'
,
));
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$this
->
assertEquals
(
'/test/?r=post/view&id=1&title=sample
%20
post'
,
$url
);
$this
->
assertEquals
(
'/test/?r=post/view&id=1&title=sample
+
post'
,
$url
);
// pretty URL without rules
// pretty URL without rules
$manager
=
new
UrlManager
(
array
(
$manager
=
new
UrlManager
(
array
(
...
@@ -29,19 +31,19 @@ class UrlManagerTest extends \yiiunit\TestCase
...
@@ -29,19 +31,19 @@ class UrlManagerTest extends \yiiunit\TestCase
'baseUrl'
=>
'/'
,
'baseUrl'
=>
'/'
,
));
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$this
->
assertEquals
(
'/post/view?
r=post/view&id=1&title=sample%20
post'
,
$url
);
$this
->
assertEquals
(
'/post/view?
id=1&title=sample+
post'
,
$url
);
$manager
=
new
UrlManager
(
array
(
$manager
=
new
UrlManager
(
array
(
'enablePrettyUrl'
=>
true
,
'enablePrettyUrl'
=>
true
,
'baseUrl'
=>
'/test/'
,
'baseUrl'
=>
'/test/'
,
));
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$this
->
assertEquals
(
'/test/post/view?
r=post/view&id=1&title=sample%20
post'
,
$url
);
$this
->
assertEquals
(
'/test/post/view?
id=1&title=sample+
post'
,
$url
);
$manager
=
new
UrlManager
(
array
(
$manager
=
new
UrlManager
(
array
(
'enablePrettyUrl'
=>
true
,
'enablePrettyUrl'
=>
true
,
'baseUrl'
=>
'/test/index.php'
,
'baseUrl'
=>
'/test/index.php'
,
));
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$this
->
assertEquals
(
'/test/index.php/post/view?
r=post/view&id=1&title=sample%20
post'
,
$url
);
$this
->
assertEquals
(
'/test/index.php/post/view?
id=1&title=sample+
post'
,
$url
);
// todo: test showScriptName
// todo: test showScriptName
...
@@ -51,12 +53,13 @@ class UrlManagerTest extends \yiiunit\TestCase
...
@@ -51,12 +53,13 @@ class UrlManagerTest extends \yiiunit\TestCase
'rules'
=>
array
(
'rules'
=>
array
(
array
(
array
(
'pattern'
=>
'post/<id>/<title>'
,
'pattern'
=>
'post/<id>/<title>'
,
'route'
=>
'post/view'
,
),
),
),
),
'baseUrl'
=>
'/'
,
'baseUrl'
=>
'/'
,
));
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$this
->
assertEquals
(
'/post/
view/1/sample%20te
st'
,
$url
);
$this
->
assertEquals
(
'/post/
1/sample+po
st'
,
$url
);
$url
=
$manager
->
createUrl
(
'post/index'
,
array
(
'page'
=>
1
));
$url
=
$manager
->
createUrl
(
'post/index'
,
array
(
'page'
=>
1
));
$this
->
assertEquals
(
'/post/index?page=1'
,
$url
);
$this
->
assertEquals
(
'/post/index?page=1'
,
$url
);
...
@@ -66,13 +69,14 @@ class UrlManagerTest extends \yiiunit\TestCase
...
@@ -66,13 +69,14 @@ class UrlManagerTest extends \yiiunit\TestCase
'rules'
=>
array
(
'rules'
=>
array
(
array
(
array
(
'pattern'
=>
'post/<id>/<title>'
,
'pattern'
=>
'post/<id>/<title>'
,
'route'
=>
'post/view'
,
),
),
),
),
'baseUrl'
=>
'/'
,
'baseUrl'
=>
'/'
,
'suffix'
=>
'.html'
,
'suffix'
=>
'.html'
,
));
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$url
=
$manager
->
createUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$this
->
assertEquals
(
'/post/
view/1/sample%20te
st.html'
,
$url
);
$this
->
assertEquals
(
'/post/
1/sample+po
st.html'
,
$url
);
$url
=
$manager
->
createUrl
(
'post/index'
,
array
(
'page'
=>
1
));
$url
=
$manager
->
createUrl
(
'post/index'
,
array
(
'page'
=>
1
));
$this
->
assertEquals
(
'/post/index.html?page=1'
,
$url
);
$this
->
assertEquals
(
'/post/index.html?page=1'
,
$url
);
}
}
...
...
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