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
1eb1809a
Commit
1eb1809a
authored
Feb 24, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finished URL testing.
parent
2c991833
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
123 additions
and
9 deletions
+123
-9
Request.php
framework/web/Request.php
+5
-0
UrlManager.php
framework/web/UrlManager.php
+7
-6
UrlManagerTest.php
tests/unit/framework/web/UrlManagerTest.php
+111
-3
No files found.
framework/web/Request.php
View file @
1eb1809a
...
@@ -409,6 +409,11 @@ class Request extends \yii\base\Request
...
@@ -409,6 +409,11 @@ class Request extends \yii\base\Request
return
$this
->
_pathInfo
;
return
$this
->
_pathInfo
;
}
}
public
function
setPathInfo
(
$value
)
{
$this
->
_pathInfo
=
trim
(
$value
,
'/'
);
}
/**
/**
* Resolves the path info part of the currently requested URL.
* Resolves the path info part of the currently requested URL.
* A path info refers to the part that is after the entry script and before the question mark (query string).
* A path info refers to the part that is after the entry script and before the question mark (query string).
...
...
framework/web/UrlManager.php
View file @
1eb1809a
...
@@ -10,7 +10,7 @@
...
@@ -10,7 +10,7 @@
namespace
yii\web
;
namespace
yii\web
;
use
Yii
;
use
Yii
;
use
\
yii\base\Component
;
use
yii\base\Component
;
/**
/**
* UrlManager handles HTTP request parsing and creation of URLs based on a set of rules.
* UrlManager handles HTTP request parsing and creation of URLs based on a set of rules.
...
@@ -141,8 +141,11 @@ class UrlManager extends Component
...
@@ -141,8 +141,11 @@ class UrlManager extends Component
return
array
(
$pathInfo
,
array
());
return
array
(
$pathInfo
,
array
());
}
else
{
}
else
{
$route
=
(
string
)
$request
->
getParam
(
$this
->
routeVar
);
$route
=
$request
->
getParam
(
$this
->
routeVar
);
return
array
(
$route
,
array
());
if
(
is_array
(
$route
))
{
$route
=
''
;
}
return
array
((
string
)
$route
,
array
());
}
}
}
}
...
@@ -230,9 +233,7 @@ class UrlManager extends Component
...
@@ -230,9 +233,7 @@ class UrlManager extends Component
public
function
getHostInfo
()
public
function
getHostInfo
()
{
{
if
(
$this
->
_hostInfo
===
null
)
{
if
(
$this
->
_hostInfo
===
null
)
{
/** @var $request \yii\web\Request */
$this
->
_hostInfo
=
Yii
::
$app
->
getRequest
()
->
getHostInfo
();
$request
=
Yii
::
$app
->
getRequest
();
$this
->
_hostInfo
=
$request
->
getHostInfo
();
}
}
return
$this
->
_hostInfo
;
return
$this
->
_hostInfo
;
}
}
...
...
tests/unit/framework/web/UrlManagerTest.php
View file @
1eb1809a
<?php
<?php
namespace
yiiunit\framework\web
;
namespace
yiiunit\framework\web
;
use
yii\web\
Application
;
use
yii\web\
Request
;
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'
=>
'/'
,
...
@@ -50,6 +49,7 @@ class UrlManagerTest extends \yiiunit\TestCase
...
@@ -50,6 +49,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// pretty URL with rules
// pretty URL with rules
$manager
=
new
UrlManager
(
array
(
$manager
=
new
UrlManager
(
array
(
'enablePrettyUrl'
=>
true
,
'enablePrettyUrl'
=>
true
,
'cacheID'
=>
false
,
'rules'
=>
array
(
'rules'
=>
array
(
array
(
array
(
'pattern'
=>
'post/<id>/<title>'
,
'pattern'
=>
'post/<id>/<title>'
,
...
@@ -66,6 +66,7 @@ class UrlManagerTest extends \yiiunit\TestCase
...
@@ -66,6 +66,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// pretty URL with rules and suffix
// pretty URL with rules and suffix
$manager
=
new
UrlManager
(
array
(
$manager
=
new
UrlManager
(
array
(
'enablePrettyUrl'
=>
true
,
'enablePrettyUrl'
=>
true
,
'cacheID'
=>
false
,
'rules'
=>
array
(
'rules'
=>
array
(
array
(
array
(
'pattern'
=>
'post/<id>/<title>'
,
'pattern'
=>
'post/<id>/<title>'
,
...
@@ -83,11 +84,118 @@ class UrlManagerTest extends \yiiunit\TestCase
...
@@ -83,11 +84,118 @@ class UrlManagerTest extends \yiiunit\TestCase
public
function
testCreateAbsoluteUrl
()
public
function
testCreateAbsoluteUrl
()
{
{
$manager
=
new
UrlManager
(
array
(
'baseUrl'
=>
'/'
,
'hostInfo'
=>
'http://www.example.com'
,
));
$url
=
$manager
->
createAbsoluteUrl
(
'post/view'
,
array
(
'id'
=>
1
,
'title'
=>
'sample post'
));
$this
->
assertEquals
(
'http://www.example.com/?r=post/view&id=1&title=sample+post'
,
$url
);
}
}
public
function
testParseRequest
()
public
function
testParseRequest
()
{
{
$manager
=
new
UrlManager
;
$request
=
new
Request
;
// default setting without 'r' param
unset
(
$_GET
[
'r'
]);
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
''
,
array
()),
$result
);
// default setting with 'r' param
$_GET
[
'r'
]
=
'site/index'
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
'site/index'
,
array
()),
$result
);
// default setting with 'r' param as an array
$_GET
[
'r'
]
=
array
(
'site/index'
);
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
''
,
array
()),
$result
);
// pretty URL without rules
$manager
=
new
UrlManager
(
array
(
'enablePrettyUrl'
=>
true
,
));
// empty pathinfo
$request
->
pathInfo
=
''
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
''
,
array
()),
$result
);
// normal pathinfo
$request
->
pathInfo
=
'site/index'
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
'site/index'
,
array
()),
$result
);
// pathinfo with module
$request
->
pathInfo
=
'module/site/index'
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
'module/site/index'
,
array
()),
$result
);
// pathinfo with trailing slashes
$request
->
pathInfo
=
'module/site/index/'
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
'module/site/index'
,
array
()),
$result
);
// pretty URL rules
$manager
=
new
UrlManager
(
array
(
'enablePrettyUrl'
=>
true
,
'cacheID'
=>
false
,
'rules'
=>
array
(
array
(
'pattern'
=>
'post/<id>/<title>'
,
'route'
=>
'post/view'
,
),
),
));
// matching pathinfo
$request
->
pathInfo
=
'post/123/this+is+sample'
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
'post/view'
,
array
(
'id'
=>
'123'
,
'title'
=>
'this+is+sample'
)),
$result
);
// matching pathinfo with trailing slashes
$request
->
pathInfo
=
'post/123/this+is+sample/'
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
'post/view'
,
array
(
'id'
=>
'123'
,
'title'
=>
'this+is+sample'
)),
$result
);
// empty pathinfo
$request
->
pathInfo
=
''
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
''
,
array
()),
$result
);
// normal pathinfo
$request
->
pathInfo
=
'site/index'
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
'site/index'
,
array
()),
$result
);
// pathinfo with module
$request
->
pathInfo
=
'module/site/index'
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
'module/site/index'
,
array
()),
$result
);
// pretty URL rules
$manager
=
new
UrlManager
(
array
(
'enablePrettyUrl'
=>
true
,
'suffix'
=>
'.html'
,
'cacheID'
=>
false
,
'rules'
=>
array
(
array
(
'pattern'
=>
'post/<id>/<title>'
,
'route'
=>
'post/view'
,
),
),
));
// matching pathinfo
$request
->
pathInfo
=
'post/123/this+is+sample.html'
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
'post/view'
,
array
(
'id'
=>
'123'
,
'title'
=>
'this+is+sample'
)),
$result
);
// matching pathinfo without suffix
$request
->
pathInfo
=
'post/123/this+is+sample'
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertFalse
(
$result
);
// empty pathinfo
$request
->
pathInfo
=
''
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
''
,
array
()),
$result
);
// normal pathinfo
$request
->
pathInfo
=
'site/index.html'
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertEquals
(
array
(
'site/index'
,
array
()),
$result
);
// pathinfo without suffix
$request
->
pathInfo
=
'site/index'
;
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertFalse
(
$result
);
}
}
}
}
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