Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
b5e4aa96
Commit
b5e4aa96
authored
Mar 27, 2011
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added a way to set requirements on the routes
$app->get('/{id}', function($id) { // ... })->assert('id', '\d+');
parent
737d4f0f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
1 deletion
+27
-1
src/Silex/Controller.php
src/Silex/Controller.php
+15
-0
tests/Silex/Tests/ControllerTest.php
tests/Silex/Tests/ControllerTest.php
+12
-1
No files found.
src/Silex/Controller.php
View file @
b5e4aa96
...
@@ -64,6 +64,21 @@ class Controller
...
@@ -64,6 +64,21 @@ class Controller
}
}
$this
->
routeName
=
$routeName
;
$this
->
routeName
=
$routeName
;
return
$this
;
}
/**
* Sets the requirement for a route variable.
*
* @param string $variable The variable name
* @param string $regexp The regexp to apply
*/
public
function
assert
(
$variable
,
$regexp
)
{
$this
->
route
->
setRequirement
(
$variable
,
$regexp
);
return
$this
;
}
}
/**
/**
...
...
tests/Silex/Tests/ControllerTest.php
View file @
b5e4aa96
...
@@ -24,7 +24,9 @@ class ControllerTest extends \PHPUnit_Framework_TestCase
...
@@ -24,7 +24,9 @@ class ControllerTest extends \PHPUnit_Framework_TestCase
public
function
testBind
()
public
function
testBind
()
{
{
$controller
=
new
Controller
(
new
Route
(
'/foo'
));
$controller
=
new
Controller
(
new
Route
(
'/foo'
));
$controller
->
bind
(
'foo'
);
$ret
=
$controller
->
bind
(
'foo'
);
$this
->
assertSame
(
$ret
,
$controller
);
$this
->
assertEquals
(
'foo'
,
$controller
->
getRouteName
());
$this
->
assertEquals
(
'foo'
,
$controller
->
getRouteName
());
}
}
...
@@ -38,4 +40,13 @@ class ControllerTest extends \PHPUnit_Framework_TestCase
...
@@ -38,4 +40,13 @@ class ControllerTest extends \PHPUnit_Framework_TestCase
$controller
->
freeze
();
$controller
->
freeze
();
$controller
->
bind
(
'bar'
);
$controller
->
bind
(
'bar'
);
}
}
public
function
testAssert
()
{
$controller
=
new
Controller
(
new
Route
(
'/foo/{bar}'
));
$ret
=
$controller
->
assert
(
'bar'
,
'\d+'
);
$this
->
assertSame
(
$ret
,
$controller
);
$this
->
assertEquals
(
array
(
'bar'
=>
'\d+'
),
$controller
->
getRoute
()
->
getRequirements
());
}
}
}
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