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
09e4229e
Commit
09e4229e
authored
May 30, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[WIP] RESTful routing syntax for UrlManager
issue #303 Here is the proposal for RESTful routing syntax:
https://gist.github.com/cebe/5674918
parent
28072740
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
1 deletion
+33
-1
UrlManager.php
framework/yii/web/UrlManager.php
+31
-1
UrlManagerTest.php
tests/unit/framework/web/UrlManagerTest.php
+2
-0
No files found.
framework/yii/web/UrlManager.php
View file @
09e4229e
...
...
@@ -43,6 +43,28 @@ class UrlManager extends Component
* array, one can use the key to represent the pattern and the value the corresponding route.
* For example, `'post/<id:\d+>' => 'post/view'`.
*
* For RESTful routing this shortcut also allows you to specify the [[UrlRule::verb|HTTP verb]]
* the rule should apply for by prepending it to the pattern, separated by a blank.
* For example, `'PUT post/<id:\d+>' => 'post/update'`.
* You may specify multiple verbs by separating them with comma
* like this: `'POST,PUT post/index' => 'post/create'`.
* Note that [[UrlRule::mode|mode]] will be set to PARSING_ONLY when specifying verb in this way
* so you normally would not specify a verb for normal GET request.
*
* Here is an example configuration for RESTful CRUD controller:
* ~~~php
* array(
* 'dashboard' => 'site/index',
*
* 'POST <controller:\w+>s' => '<controller>/create',
* '<controller:\w+>s' => '<controller>/index',
*
* 'PUT <controller:\w+>/<id:\d+>' => '<controller>/update',
* 'DELETE <controller:\w+>/<id:\d+>' => '<controller>/delete',
* '<controller:\w+>/<id:\d+>' => '<controller>/view',
* );
* ~~~
*
* Note that if you modify this property after the UrlManager object is created, make sure
* you populate the array with rule objects instead of rule configurations.
*/
...
...
@@ -115,9 +137,17 @@ class UrlManager extends Component
foreach
(
$this
->
rules
as
$key
=>
$rule
)
{
if
(
!
is_array
(
$rule
))
{
$rule
=
array
(
'pattern'
=>
$key
,
'route'
=>
$rule
,
);
if
((
$pos
=
strpos
(
$key
,
' '
))
!==
false
)
{
$verbs
=
substr
(
$key
,
0
,
$pos
);
if
(
preg_match
(
'/^((GET|POST|PUT|DELETE),?)*$/'
,
$verbs
,
$matches
))
{
$rule
[
'verb'
]
=
explode
(
','
,
$verbs
);
$rule
[
'mode'
]
=
UrlRule
::
PARSING_ONLY
;
$key
=
substr
(
$key
,
$pos
+
1
);
}
}
$rule
[
'pattern'
]
=
$key
;
}
$rules
[]
=
Yii
::
createObject
(
array_merge
(
$this
->
ruleConfig
,
$rule
));
}
...
...
tests/unit/framework/web/UrlManagerTest.php
View file @
09e4229e
...
...
@@ -248,4 +248,6 @@ class UrlManagerTest extends TestCase
$result
=
$manager
->
parseRequest
(
$request
);
$this
->
assertFalse
(
$result
);
}
// TODO test RESTful pattern syntax e.g. 'GET index' => 'site/index'
}
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