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
e25ad4bc
Commit
e25ad4bc
authored
Feb 11, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
URL wip.
parent
a946a863
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
17 deletions
+77
-17
UrlRule.php
framework/web/UrlRule.php
+33
-17
UrlRuleTest.php
tests/unit/framework/web/UrlRuleTest.php
+44
-0
No files found.
framework/web/UrlRule.php
View file @
e25ad4bc
<?php
<?php
/**
/**
* Url
Manager
class file
* Url
Rule
class file
*
*
* @link http://www.yiiframework.com/
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008 Yii Software LLC
* @copyright Copyright © 2008 Yii Software LLC
...
@@ -12,18 +12,7 @@ namespace yii\web;
...
@@ -12,18 +12,7 @@ namespace yii\web;
use
yii\base\Object
;
use
yii\base\Object
;
/**
/**
* UrlManager manages the URLs of Yii applications.
* UrlRule represents a rule used for parsing and generating URLs.
* array(
* 'pattern' => 'post/<page:\d+>',
* 'route' => 'post/view',
* 'defaults' => array('page' => 1),
* )
*
* array(
* 'pattern' => 'about',
* 'route' => 'site/page',
* 'defaults' => array('view' => 'about'),
* )
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
...
@@ -45,13 +34,36 @@ class UrlRule extends Object
...
@@ -45,13 +34,36 @@ class UrlRule extends Object
*/
*/
public
$defaults
=
array
();
public
$defaults
=
array
();
protected
$paramRules
=
array
();
/**
protected
$routeRule
;
* @var string the template for generating a new URL. This is derived from [[pattern]] and is used in generating URL.
*/
protected
$template
;
protected
$template
;
/**
* @var string the regex for matching the route part. This is used in generating URL.
*/
protected
$routeRule
;
/**
* @var array list of regex for matching parameters. This is used in generating URL.
*/
protected
$paramRules
=
array
();
/**
* @var array list of parameters used in the route.
*/
protected
$routeParams
=
array
();
protected
$routeParams
=
array
();
/**
* Initializes this rule.
*/
public
function
init
()
public
function
init
()
{
{
$this
->
compileRule
();
}
/**
* Compiles the rule using the current configuration.
*/
protected
function
compileRule
()
{
$this
->
pattern
=
trim
(
$this
->
pattern
,
'/'
);
$this
->
pattern
=
trim
(
$this
->
pattern
,
'/'
);
if
(
$this
->
pattern
===
''
)
{
if
(
$this
->
pattern
===
''
)
{
$this
->
template
=
''
;
$this
->
template
=
''
;
...
@@ -105,17 +117,21 @@ class UrlRule extends Object
...
@@ -105,17 +117,21 @@ class UrlRule extends Object
if
(
!
preg_match
(
$this
->
pattern
,
$pathInfo
,
$matches
))
{
if
(
!
preg_match
(
$this
->
pattern
,
$pathInfo
,
$matches
))
{
return
false
;
return
false
;
}
}
foreach
(
$this
->
defaults
as
$name
=>
$value
)
{
if
(
!
isset
(
$matches
[
$name
])
||
$matches
[
$name
]
===
''
)
{
$matches
[
$name
]
=
$value
;
}
}
$params
=
$this
->
defaults
;
$params
=
$this
->
defaults
;
$tr
=
array
();
$tr
=
array
();
foreach
(
$matches
as
$name
=>
$value
)
{
foreach
(
$matches
as
$name
=>
$value
)
{
if
(
$value
!==
''
)
{
if
(
isset
(
$this
->
routeParams
[
$name
]))
{
if
(
isset
(
$this
->
routeParams
[
$name
]))
{
$tr
[
$this
->
routeParams
[
$name
]]
=
$value
;
$tr
[
$this
->
routeParams
[
$name
]]
=
$value
;
unset
(
$params
[
$name
]);
}
elseif
(
isset
(
$this
->
paramRules
[
$name
]))
{
}
elseif
(
isset
(
$this
->
paramRules
[
$name
]))
{
$params
[
$name
]
=
$value
;
$params
[
$name
]
=
$value
;
}
}
}
}
}
if
(
$this
->
routeRule
!==
null
)
{
if
(
$this
->
routeRule
!==
null
)
{
$route
=
strtr
(
$this
->
route
,
$tr
);
$route
=
strtr
(
$this
->
route
,
$tr
);
}
else
{
}
else
{
...
...
tests/unit/framework/web/UrlRuleTest.php
View file @
e25ad4bc
...
@@ -435,6 +435,50 @@ class UrlRuleTest extends \yiiunit\TestCase
...
@@ -435,6 +435,50 @@ class UrlRuleTest extends \yiiunit\TestCase
array
(
'post'
,
false
),
array
(
'post'
,
false
),
),
),
),
),
array
(
'route has parameters'
,
array
(
'pattern'
=>
'<controller>/<action>'
,
'route'
=>
'<controller>/<action>'
,
'defaults'
=>
array
(),
),
array
(
array
(
'post/index'
,
'post/index'
),
array
(
'module/post/index'
,
false
),
),
),
array
(
'route has parameters with regex'
,
array
(
'pattern'
=>
'<controller:post|comment>/<action>'
,
'route'
=>
'<controller>/<action>'
,
'defaults'
=>
array
(),
),
array
(
array
(
'post/index'
,
'post/index'
),
array
(
'comment/index'
,
'comment/index'
),
array
(
'test/index'
,
false
),
array
(
'post'
,
false
),
array
(
'module/post/index'
,
false
),
),
),
array
(
'route has default parameter'
,
array
(
'pattern'
=>
'<controller:post|comment>/<action>'
,
'route'
=>
'<controller>/<action>'
,
'defaults'
=>
array
(
'action'
=>
'index'
),
),
array
(
array
(
'post/view'
,
'post/view'
),
array
(
'comment/view'
,
'comment/view'
),
array
(
'test/view'
,
false
),
array
(
'post'
,
'post/index'
),
array
(
'posts'
,
false
),
array
(
'test'
,
false
),
array
(
'index'
,
false
),
),
),
);
);
}
}
}
}
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