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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
1a71c109
Commit
1a71c109
authored
Mar 21, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added `yii\web\PrefixUrlRule`
parent
a9546a99
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
357 additions
and
1 deletion
+357
-1
CHANGELOG.md
framework/CHANGELOG.md
+2
-1
PrefixUrlRule.php
framework/web/PrefixUrlRule.php
+138
-0
PrefixUrlRuleTest.php
tests/unit/framework/web/PrefixUrlRuleTest.php
+217
-0
No files found.
framework/CHANGELOG.md
View file @
1a71c109
...
...
@@ -260,7 +260,8 @@ Yii Framework 2 Change Log
-
New #2149: Added
`yii\base\DynamicModel`
to support ad-hoc data validation (qiangxue)
-
New #2360: Added
`AttributeBehavior`
and
`BlameableBehavior`
, and renamed
`AutoTimestamp`
to
`TimestampBehavior`
(lucianobaraglia, qiangxue)
-
New: Yii framework now comes with core messages in multiple languages
-
New: Added yii
\c
odeception
\D
bTestCase (qiangxue)
-
New: Added
`yii\codeception\DbTestCase`
(qiangxue)
-
New: Added
`yii\web\PrefixUrlRule`
(qiangxue)
2.0.0 alpha, December 1, 2013
...
...
framework/web/PrefixUrlRule.php
0 → 100644
View file @
1a71c109
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\web
;
use
Yii
;
use
yii\base\InvalidConfigException
;
/**
* PrefixUrlRule represents a collection of URL rules sharing the same prefix in their patterns and routes.
*
* PrefixUrlRule is best used by a module which often uses module ID as the prefix for the URL rules.
* For example, the following code creates a rule for the `admin` module:
*
* ```php
* new PrefixUrlRule([
* 'prefix' => 'admin',
* 'rules' => [
* 'login' => 'user/login',
* 'logout' => 'user/logout',
* 'dashboard' => 'default/dashboard',
* ],
* ]);
*
* // the above rule is equivalent to the following three rules:
*
* [
* 'admin/login' => 'admin/user/login',
* 'admin/logout' => 'admin/user/logout',
* 'admin/dashboard' => 'admin/default/dashboard',
* ]
* ```
*
* The above example assumes the prefix for patterns and routes are the same. They can be made different
* by configuring [[prefix]] and [[routePrefix]] separately.
*
* Using a PrefixUrlRule is more efficient than directly declaring the individual rules its contains.
* This is because PrefixUrlRule can quickly determine if it should process a URL parsing or creation request
* by simply checking if the prefix matches.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
PrefixUrlRule
extends
CompositeUrlRule
{
/**
* @var array the rules contained within this composite rule. Please refer to [[UrlManager::rules]]
* for the format of this property.
* @see prefix
* @see routePrefix
*/
public
$rules
=
[];
/**
* @var string the prefix for the pattern part of every rule declared in [[rules]].
* The prefix and the pattern will be separated with a slash.
*/
public
$prefix
;
/**
* @var string the prefix for the route part of every rule declared in [[rules]].
* The prefix and the route will be separated with a slash.
* If this property is not set, it will take the value of [[prefix]].
*/
public
$routePrefix
;
/**
* @var array the default configuration of URL rules. Individual rule configurations
* specified via [[rules]] will take precedence when the same property of the rule is configured.
*/
public
$ruleConfig
=
[
'class'
=>
'yii\web\UrlRule'
];
/**
* @inheritdoc
*/
public
function
init
()
{
if
(
$this
->
routePrefix
===
null
)
{
$this
->
routePrefix
=
$this
->
prefix
;
}
$this
->
prefix
=
trim
(
$this
->
prefix
,
'/'
);
$this
->
routePrefix
=
trim
(
$this
->
routePrefix
,
'/'
);
parent
::
init
();
}
/**
* @inheritdoc
*/
protected
function
createRules
()
{
$rules
=
[];
foreach
(
$this
->
rules
as
$key
=>
$rule
)
{
if
(
!
is_array
(
$rule
))
{
$rule
=
[
'pattern'
=>
ltrim
(
$this
->
prefix
.
'/'
.
$key
,
'/'
),
'route'
=>
ltrim
(
$this
->
routePrefix
.
'/'
.
$rule
,
'/'
),
];
}
elseif
(
isset
(
$rule
[
'pattern'
],
$rule
[
'route'
]))
{
$rule
[
'pattern'
]
=
ltrim
(
$this
->
prefix
.
'/'
.
$rule
[
'pattern'
],
'/'
);
$rule
[
'route'
]
=
ltrim
(
$this
->
routePrefix
.
'/'
.
$rule
[
'route'
],
'/'
);
}
$rule
=
Yii
::
createObject
(
array_merge
(
$this
->
ruleConfig
,
$rule
));
if
(
!
$rule
instanceof
UrlRuleInterface
)
{
throw
new
InvalidConfigException
(
'URL rule class must implement UrlRuleInterface.'
);
}
$rules
[]
=
$rule
;
}
return
$rules
;
}
/**
* @inheritdoc
*/
public
function
parseRequest
(
$manager
,
$request
)
{
$pathInfo
=
$request
->
getPathInfo
();
if
(
$this
->
prefix
===
''
||
strpos
(
$pathInfo
.
'/'
,
$this
->
prefix
.
'/'
)
===
0
)
{
return
parent
::
parseRequest
(
$manager
,
$request
);
}
else
{
return
false
;
}
}
/**
* @inheritdoc
*/
public
function
createUrl
(
$manager
,
$route
,
$params
)
{
if
(
$this
->
routePrefix
===
''
||
strpos
(
$route
,
$this
->
routePrefix
.
'/'
)
===
0
)
{
return
parent
::
createUrl
(
$manager
,
$route
,
$params
);
}
else
{
return
false
;
}
}
}
tests/unit/framework/web/PrefixUrlRuleTest.php
0 → 100644
View file @
1a71c109
<?php
namespace
yiiunit\framework\web
;
use
yii\web\UrlManager
;
use
yii\web\UrlRule
;
use
yii\web\PrefixUrlRule
;
use
yii\web\Request
;
use
yiiunit\TestCase
;
/**
* @group web
*/
class
PrefixUrlRuleTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testCreateUrl
()
{
$manager
=
new
UrlManager
([
'cache'
=>
null
]);
$suites
=
$this
->
getTestsForCreateUrl
();
foreach
(
$suites
as
$i
=>
$suite
)
{
list
(
$name
,
$config
,
$tests
)
=
$suite
;
$rule
=
new
PrefixUrlRule
(
$config
);
foreach
(
$tests
as
$j
=>
$test
)
{
list
(
$route
,
$params
,
$expected
)
=
$test
;
$url
=
$rule
->
createUrl
(
$manager
,
$route
,
$params
);
$this
->
assertEquals
(
$expected
,
$url
,
"Test#
$i
-
$j
:
$name
"
);
}
}
}
public
function
testParseRequest
()
{
$manager
=
new
UrlManager
([
'cache'
=>
null
]);
$request
=
new
Request
([
'hostInfo'
=>
'http://en.example.com'
]);
$suites
=
$this
->
getTestsForParseRequest
();
foreach
(
$suites
as
$i
=>
$suite
)
{
list
(
$name
,
$config
,
$tests
)
=
$suite
;
$rule
=
new
PrefixUrlRule
(
$config
);
foreach
(
$tests
as
$j
=>
$test
)
{
$request
->
pathInfo
=
$test
[
0
];
$route
=
$test
[
1
];
$params
=
isset
(
$test
[
2
])
?
$test
[
2
]
:
[];
$result
=
$rule
->
parseRequest
(
$manager
,
$request
);
if
(
$route
===
false
)
{
$this
->
assertFalse
(
$result
,
"Test#
$i
-
$j
:
$name
"
);
}
else
{
$this
->
assertEquals
([
$route
,
$params
],
$result
,
"Test#
$i
-
$j
:
$name
"
);
}
}
}
}
protected
function
getTestsForCreateUrl
()
{
// structure of each test
// message for the test
// config for the URL rule
// list of inputs and outputs
// route
// params
// expected output
return
[
[
'no prefix'
,
[
'rules'
=>
[
'login'
=>
'user/login'
,
'logout'
=>
'user/logout'
,
],
],
[
[
'user/login'
,
[],
'login'
],
[
'user/logout'
,
[],
'logout'
],
[
'user/create'
,
[],
false
],
],
],
[
'prefix only'
,
[
'prefix'
=>
'admin'
,
'rules'
=>
[
'login'
=>
'user/login'
,
'logout'
=>
'user/logout'
,
],
],
[
[
'admin/user/login'
,
[],
'admin/login'
],
[
'admin/user/logout'
,
[],
'admin/logout'
],
[
'user/create'
,
[],
false
],
],
],
[
'prefix and routePrefix different'
,
[
'prefix'
=>
'_'
,
'routePrefix'
=>
'admin'
,
'rules'
=>
[
'login'
=>
'user/login'
,
'logout'
=>
'user/logout'
,
],
],
[
[
'admin/user/login'
,
[],
'_/login'
],
[
'admin/user/logout'
,
[],
'_/logout'
],
[
'user/create'
,
[],
false
],
],
],
[
'ruleConfig with suffix'
,
[
'prefix'
=>
'_'
,
'routePrefix'
=>
'admin'
,
'ruleConfig'
=>
[
'suffix'
=>
'.html'
,
'class'
=>
'yii\\web\\UrlRule'
],
'rules'
=>
[
'login'
=>
'user/login'
,
'logout'
=>
'user/logout'
,
],
],
[
[
'admin/user/login'
,
[],
'_/login.html'
],
[
'admin/user/logout'
,
[],
'_/logout.html'
],
[
'user/create'
,
[],
false
],
],
],
];
}
protected
function
getTestsForParseRequest
()
{
// structure of each test
// message for the test
// config for the URL rule
// list of inputs and outputs
// pathInfo
// expected route, or false if the rule doesn't apply
// expected params, or not set if empty
return
[
[
'no prefix'
,
[
'rules'
=>
[
'login'
=>
'user/login'
,
'logout'
=>
'user/logout'
,
],
],
[
[
'login'
,
'user/login'
],
[
'logout'
,
'user/logout'
],
[
'create'
,
false
],
],
],
[
'prefix only'
,
[
'prefix'
=>
'admin'
,
'rules'
=>
[
'login'
=>
'user/login'
,
'logout'
=>
'user/logout'
,
],
],
[
[
'admin/login'
,
'admin/user/login'
],
[
'admin/logout'
,
'admin/user/logout'
],
[
'admin/create'
,
false
],
[
'create'
,
false
],
],
],
[
'prefix and routePrefix different'
,
[
'prefix'
=>
'_'
,
'routePrefix'
=>
'admin'
,
'rules'
=>
[
'login'
=>
'user/login'
,
'logout'
=>
'user/logout'
,
],
],
[
[
'_/login'
,
'admin/user/login'
],
[
'_/logout'
,
'admin/user/logout'
],
[
'_/create'
,
false
],
[
'create'
,
false
],
],
],
[
'ruleConfig with suffix'
,
[
'prefix'
=>
'_'
,
'routePrefix'
=>
'admin'
,
'ruleConfig'
=>
[
'suffix'
=>
'.html'
,
'class'
=>
'yii\\web\\UrlRule'
],
'rules'
=>
[
'login'
=>
'user/login'
,
'logout'
=>
'user/logout'
,
],
],
[
[
'_/login.html'
,
'admin/user/login'
],
[
'_/logout.html'
,
'admin/user/logout'
],
[
'_/logout'
,
false
],
[
'_/create.html'
,
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